1

ギャラリーやカメラの画像を操作する小さなアプリを作成しました。

それはすべて正常に動作しますが、. 小さな画面と小さなメモリ サイズのデバイス (HTC Desire) では、他の携帯電話からフル サイズでダウンロードした画像がいくつかありますが、それらははるかに大きくなっています (その電話の 8MP カメラ)。

それを読み込もうとすると、小さなカメラの巨大な画像の場合、すぐにクラッシュします。

では、ある種のチェックを実装してその画像を縮小する方法はありますが、適切にロードする方法はありますか?

イメージがロードされた後に縮小しますが、これはクラッシュが発生する前に実行する必要があります。

Tnx。

           InputStream in = null;
            try {
                in = getContentResolver().openInputStream(data.getData());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // get picture size.
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, options);
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // resize the picture for memory.
            int screenH = getResources().getDisplayMetrics().heightPixels; //800
            int screenW = getResources().getDisplayMetrics().widthPixels; //480
            int width = options.outWidth / screenW;
            int height = options.outHeight / screenH;

            Log.w("Screen Width", Integer.toString(width));
            Log.w("Screen Height", Integer.toString(height));

            int sampleSize = Math.max(width, height);
            options.inSampleSize = sampleSize;
            options.inJustDecodeBounds = false;
            try {
                in = getContentResolver().openInputStream(data.getData());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // convert to bitmap with declared size.
            Globals.INSTANCE.imageBmp = BitmapFactory.decodeStream(in, null, options);
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
4

1 に答える 1

1

設定するだけでビットマップをメモリにロードすることを避けることができます

inJustDecodeBounds = true

inJustDecodeBoundsを使用すると、画像をデコードせずに境界のみをデコードできます。与えられheightwidthビットマップを使用してダウンサンプリングできます。

サンプルサイズ

ドキュメントのまま:

1 より大きい値に設定すると、メモリを節約するために小さいイメージを返し、元のイメージをサブサンプリングするようにデコーダに要求します。

int tmpWidth = bitmapWidth;
int tmpHeight = bitmapHeigth;
int requiredSize = ...
while (true) {
 if (tmpWidth / 2 < requiredSize
    || tmpHeight / 2 < requiredSize)
        break;
    tmpWidth /= 2;
    tmpHeight /= 2;
    ratio *= 2;
 }

編集:32ビットBitmapの場合、必要なメモリは width * height * 4

于 2012-10-02T12:04:07.847 に答える