SO を検索して多くの回答を読んだ後、次のコードを作成してinSampleSize
、大きなビットマップをダウンサンプリングするための最小値を取得しました。
public Bitmap load(Context context, String image_url) throws Exception {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image_url, bitmapOptions);
final float imageSize = (float) bitmapOptions.outWidth * (float) bitmapOptions.outHeight * 4.0f / 1024.0f / 1024.0f; // MB
bitmapOptions.inSampleSize = (int) Math.pow(2, Math.floor(imageSize / MemoryManagement.free(context)));
bitmapOptions.inJustDecodeBounds = false;
return BitmapProcessing.modifyOrientation(BitmapFactory.decodeFile(image_url, bitmapOptions), image_url);
}
と
public class MemoryManagement {
public static float free(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass() - 10;
if (memoryClass < 1) memoryClass = 1;
return (float) memoryClass;
}
}
目標は、OutOfMemory 例外なしでビットマップ サンプルの最大サイズを取得することです。このコードを信頼できますか?