转:解决通过Intent调用系统拍照程序,返回图片太小的问题[android]
以下的代码可以调用系统的拍照程序, | Intent it = newIntent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER); | 按下拍照键后,会返回到你的activity,所以你的activity要在onActivityResult方法里加一个处理, | protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Bundle extras = data.getExtras(); Bitmap b = (Bitmap) extras.get("data"); ImageView img = (ImageView)findViewById(R.id.image); img.setImageBitmap(take); | 但是这样你会发现这个bitmap尺寸太小了。明显是被压缩过了,要像返回未被压缩的照片,那么你要给调用系统拍照程序intent加上参数,指定图片输出的位置。 | 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); | 这样就是大图片返回了。 | protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); ImageView img = (ImageView)findViewById(R.id.image); take = U.ResizeBitmap(U.getBitmapForFile(F.SD_CARD_TEMP_PHOTO_PATH), 640); img.setImageBitmap(take); | 另外注意一下,返回的那个bitmap会很大,你用完以后要把它回收掉,不然你很容易内存报oom错误 | 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(); matrix.postScale(scaleWidth, scaleHeight); // matrix.postRotate(45); Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); | |