我在建一个android在哪里.在一个活动里面我有一个图像按钮.当我点击它时,画廊打开,我可以选择一个图像.然后我将该图像设置为图像按钮的新图像.
问题是图像在我的活动中看起来太大了.如何使其适合我的图像按钮?
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
mImageButton.setImageBitmap(yourSelectedImage);
}
}
}
解决方法:
您可以使用此方法获取已调整大小的图像.这样就可以避免OutOfMemoryError
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);
}