0

このコードを使用して、ギャラリーからビットマップに画像を読み込もうとしています:

Bitmap myBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(adress), (int)viewWidth/2, (int)viewHeight/2, false);

ここで、'adress' はギャラリー内の画像のアドレスです。これは、Galaxy Note 1 の Android バージョン 2.3.6 では問題なく動作しますが、Galaxy 2 の Android バージョン 4.1.2 では「メモリ不足」のためにクラッシュします。

ここで何か間違ったことをしていますか?スケーリングされたビットマップを取得する方法はありますか? 結果のビットマップ (これが機能する場合) は、スケーリングのために少し汚れていますが、気にしません。ありがとう!!!

4

2 に答える 2

2

この方法を使用する

    Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);

実装-

     public int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
        int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(path, options);
    return bmp;
}

}

于 2013-07-19T12:51:48.820 に答える