2

SDカードに画像を保存しています(各サイズ〜4MB)。

ImageViewに設定するよりも、それぞれのサイズを変更したい。

BitmapFactory.decodeFile(path)ただ例外が出ているので使えません
java.lang.OutOfMemoryError

メモリにロードせずに画像のサイズを変更するにはどうすればよいですか。それは本当ですか?

4

2 に答える 2

4

ビットマップオプションを使用する:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565; //Use this if you dont require Alpha channel
options.inSampleSize = 4; // The higher, the smaller the image size and resolution read in

次に、デコードでオプションを設定します

BitmapFactory.decodeFile(path, options)

これは、ビットマップを効率的に表示する方法について読むための良いリンクです。

このようなメソッドを記述して、希望の解像度でサイズの画像を取得することもできます。

次の方法では、画像のサイズを確認してから、サンプルサイズでファイルからデコードし、メモリ使用量を低く抑えながら、それに応じてsdcardから画像のサイズを変更します。

   public static Bitmap decodeSampledBitmapFromFile(string path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options);
  }
于 2012-08-06T15:26:27.760 に答える
0

使用する前にスケーリングする必要がありBitmapます。これにより、メモリ消費を減らすことができます。

これを見てください、それはあなたを助けるかもしれません。

そして、あなたがもう彼を必要としないならば、あなたrecycleにそれを確かめてください。Bitmap

于 2012-08-06T15:32:27.023 に答える