Android 如何在从大位图中采样图像时使用URI而不是resID?
在imageView中将2100万像素图像作为位图加载时,我的应用程序抛出内存不足异常Android 如何在从大位图中采样图像时使用URI而不是resID?,android,Android,在imageView中将2100万像素图像作为位图加载时,我的应用程序抛出内存不足异常 Uri imageUri = /* URI goes here */ Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); Imageview iv = (Imageview) findViewById (R.id.my_img_view); iv.setImageBitmap(bitma
Uri imageUri = /* URI goes here */
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Imageview iv = (Imageview) findViewById (R.id.my_img_view);
iv.setImageBitmap(bitmap);
所以我在android官方文档中找到了这个页面。它对图像进行降采样,并在内存中加载较小的版本,而不是加载整个图像
问题是这段代码使用了ResID而不是URI
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
如何将此代码与我已有的URI一起使用 使用
BitmapFactory.decodeStream(…)
而不是BitmapFactory.DecodeSource(…)
:
InputStream inputStream = context.getContentResolver().openInputStream(uri);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);