我需要以较低的分辨率加载图像。我该怎么办?
我累了下一个代码:
public Bitmap decodeSampledBitmapFromUri(@NonNull Uri Uri, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
InputStream is = getContentResolver().openInputStream(Uri);
BitmapFactory.decodeStream(is, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // I can read width and height from options
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(is, null, options);
} catch (Exception ignored)
{
}
return null;
}
而且我什至可以看到加载图像的宽度和高度。但是当我将BitmapFactory.decodeStream
设为false时,null
返回inJustDecodeBounds
。怎么了?
用户从图库中选择的图像。授予所有权限。
您的InputStream
被第一个decodeStream()
关闭。打开一个新的InputStream
以与第二个decodeStream()
一起使用。