0

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 例外なしでビットマップ サンプルの最大サイズを取得することです。このコードを信頼できますか?

4

2 に答える 2

0

ヒープサイズが16MBに制限されている場合、Nadirが返すのは1MBです。つまり、たとえば20MBの画像を1MBのスペースにスケーリングする必要がありますが、たとえば10MBを使用できるため、私のソリューションは動的にスケーリングされます。ヒープ サイズは 16MB または 24MB などです。

    public class MemoryManagement {

    public static float free(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        int memoryClass = am.getMemoryClass();
        memoryClass = ((memoryClass *3)/5); // use 0.6 of your memory 
        if (memoryClass < 1) memoryClass = 1;
        return (float) memoryClass;
      }
   }
于 2014-09-17T19:37:13.417 に答える