0
小さいバージョンをメモリに読み込むためにビットマップを縮小しようとしています。私は、リソースの代わりにイメージギャラリーからロードすることを除いて、Googleの例(検索は大きなビットマップを効率的に読み込む)にかなり従っています。しかし、私は次元を計算した後にヌルビットマップを取り戻しているようです。私のコードは以下の通りです:ビットマップの縮小版をロードしようとしたときにヌル
/** OnActivityResult Method **/
final Uri imageUri = data.getData();
final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
Bitmap bitmapToLoad = Util.decodeSampledBitmapFromResource(imageStream, 500, 500); // bitmapToLoad is null.
mIvScreenshot.setImageBitmap(bitmapToLoad);
/**Helper Methods **/
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) >= reqHeight
&& (halfWidth/inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(InputStream is,
int reqWidth, int reqHeight) {
Rect rect = new Rect();
// First decode with inJustDecodeBounds = true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, rect, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(is, rect, options);
}
誰かが間違っていることをキャッチすることはできますか?
あなたはURIを取得し、入力ストリームを開くことができますか?最終的なUri imageUri = data.getData(); final InputStream imageStream = getActivity()。getContentResolver()。openInputStream(imageUri);? – Juan
https://stackoverflow.com/a/13872663/ 833647入力ストリームを「実際に」読む前にリセットする必要があります。 –