孤海傲月

导航

解决通过Intent调用系统拍照程序,返回图片太小的问题[android] 【转】

以下的代码可以调用系统的拍照程序,

1
2
Intent it = newIntent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);

 

按下拍照键后,会返回到你的activity,所以你的activity要在onActivityResult方法里加一个处理,

1
2
3
4
5
6
7
8
9
10
11
12
13
protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
take = b;
ImageView img = (ImageView)findViewById(R.id.image);
img.setImageBitmap(take);
}catch(Exception e){
 
}
 
}

但是这样你会发现这个bitmap尺寸太小了。明显是被压缩过了,要像返回未被压缩的照片,那么你要给调用系统拍照程序intent加上参数,指定图片输出的位置。

 

1
2
3
Intent it = newIntent("android.media.action.IMAGE_CAPTURE");
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile(F.SD_CARD_TEMP_PHOTO_PATH)));
startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);

 

这样就是大图片返回了。

1
2
3
4
5
6
7
8
9
10
11
12
protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
ImageView img = (ImageView)findViewById(R.id.image);
take = U.ResizeBitmap(U.getBitmapForFile(F.SD_CARD_TEMP_PHOTO_PATH), 640);
img.setImageBitmap(take);
imgflag = true;
}catch(Exception e){
 
}
 
}

 

另外注意一下,返回的那个bitmap会很大,你用完以后要把它回收掉,不然你很容易内存报oom错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
publicstaticBitmap ResizeBitmap(Bitmap bitmap, intnewWidth) {
 
intwidth = bitmap.getWidth();
intheight = bitmap.getHeight();
floattemp = ((float) height) / ((float) width);
intnewHeight = (int) ((newWidth) * temp);
floatscaleWidth = ((float) newWidth) / width;
floatscaleHeight = ((float) newHeight) / height;
Matrix matrix = newMatrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// matrix.postRotate(45);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
bitmap.recycle();
returnresizedBitmap;
 
}

posted on 2013-04-24 17:17  孤海傲月  阅读(744)  评论(0编辑  收藏  举报