我制作了一个用户从图库中选择图片的应用程序。但是当用户选择从相机中拍摄的图像时,我得到了内存错误"。我尝试缩小尺寸但没有成功。请帮助我
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult (requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
selectedImage = data.getData ();
try {
decodeUri (getApplicationContext (),selectedImage,100);
} catch (FileNotFoundException e) {
e.printStackTrace ();
}
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver ().query (selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst ();
int columnIndex = cursor.getColumnIndex (filePathColumn[0]);
picturePath = cursor.getString (columnIndex);
drawable = Drawable.createFromPath (picturePath);
cursor.close ();
dialog = new Dialog (CaptureImage.this,android.R.style.Theme_DeviceDefault);
dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
dialog.setContentView (R.layout.confirmation_image);
dialog.getWindow ().setLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ImageView image = (ImageView) dialog.findViewById (R.id.iv_selected_image);
image.setImageDrawable (drawable);
ok_gallery = (Button) dialog.findViewById (R.id.bt_ok_gallery);
cancel = (Button) dialog.findViewById (R.id.bt_cancel);
ok_gallery.setOnClickListener (this);
cancel.setOnClickListener (this);
dialog.show ();
decodeUri Code
public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);
int width_tmp = o.outWidth
, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
}