Android拍照并储存 各种方法汇总

本文介绍了在Android应用中实现拍照功能及图片处理的方法,包括如何使用ContentProvider和Intent启动相机,自定义图片保存路径,以及根据屏幕尺寸调整显示图片大小等关键技术。

拍照并储存

1、使用Contentprovider和Intent       

http://www.cnblogs.com/android-html5/archive/2011/12/18/2533962.html

2、多媒体的获取

http://blog.youkuaiyun.com/xingtian713/article/details/6525411

3、拍照并获取,采用OpenInputStream(Uri)

http://blog.youkuaiyun.com/chenjie19891104/article/details/6320323

4、检索android图片库

http://blog.youkuaiyun.com/chenjie19891104/article/details/6320664

5、获取

http://hi.baidu.com/knuuy/item/96edab1d5c72258c89a9566b

获取+检索

http://blog.youkuaiyun.com/asce1885/article/details/7082068

public class SizedCameraActivity extends Activity {
	final static int CAMERA_RESULT = 0;
	ImageView iv = null;
	String imageFilePath = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
//设置,储存路径
		String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + 
				"/myfavoritepicture.jpg";
		File imageFile = new File(imageFilePath);
		Uri imageFileUri = Uri.fromFile(imageFile);
//设置Intent
		Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//给Intent添加参数,保存到指定目录
		it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
		startActivityForResult(it, CAMERA_RESULT);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (RESULT_OK == resultCode) {
			iv = (ImageView) findViewById(R.id.ReturnedImageView);
			//因为,拍照获得的图像的长宽,过大,倒是,ImageView放不下,所以需要根据屏幕大小比例调整,图像大小,来使ImageVIew显示
			Display currentDisplay = getWindowManager().getDefaultDisplay();
			int dw = currentDisplay.getWidth();
			int dh = currentDisplay.getHeight();

			// Load up the image's dimensions not the image itself
			BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
//设置BitmapFactory的参数,只计算出图像的大小,而不实际进行图像的解码:
			bmpFactoryOptions.inJustDecodeBounds = true;
			Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
			int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
			int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
			Log.v("HEIGHTRATIO", "" + heightRatio);
			Log.v("WIDTHRATIO", "" + widthRatio);
//根据长宽中,较大的来设置比例
			if ((heightRatio > 1) && (widthRatio > 1)) {
				if (heightRatio > widthRatio) {
					// Height ratio is larger, scale according to it
					bmpFactoryOptions.inSampleSize = heightRatio;
				} else {
					// Width ratio is larger, scale according to it 
					bmpFactoryOptions.inSampleSize = widthRatio;
				}
			}

			// Decode it for real
			bmpFactoryOptions.inJustDecodeBounds = false;

//从sd目录中读取到Bitmap
			bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

			// Display it
			iv.setImageBitmap(bmp);
		}	
	}
}

几种获取到照相机的图片的方法

使用openInputStream(Uri),参考链接3

ContentValues values = new ContentValues();  
Uri picUri=MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);//参考链接2
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFilePath);
startActivityForResult(intent, RESULT_CODE);

onActivityResult{
BitmapFactory.Options op = new BitmapFactory.Options();  
Bitmap pic = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(picUri),null,op); 
}

使用cursor+query,参考链接5

ContentValues values = new ContentValues();  
photoUri=this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);


onActivityResult{
ContentResolver cr = this.getContentResolver();  
     Cursor cursor = cr.query(photoUri, null, null, null, null);  
     cursor.moveToFirst(); //游标移到第一条就是系统默认的路径
     if (cursor != null) {  
     String getFilePath = cursor.getString(1);  //获取到的路径可以用于图片自定义压缩了
         cursor.close(); 
}

使用指定的目录来保存,直接保存

String imageFilePath =Environment.getExternalStorageDirectory().getAbsolutePath() +   "/myfavoritepicture.jpg";  
File imageFile = new File(imageFilePath);  
Uri imageFileUri = Uri.fromFile(imageFile);
Intent it=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);  
startActivityForResult(it, CAMERA_RESULT);  

参考http://blog.youkuaiyun.com/asce1885/article/details/7081633


保存Bitmap

String path=Environment.getExternalStorageDirectory().toString();
			//ff.png是将要存储的图片的名称
			  File file=new File(path, "ff.png");
			//从资源文件中选择一张图片作为将要写入的源文件
			  try {
			   FileOutputStream out=new FileOutputStream(file);
			   bttt.compress(CompressFormat.PNG, 100, out);
			   out.flush();
			   out.close();
			  } catch (Exception e) {
			   e.printStackTrace();
			  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值