1

Android用のカードゲームを開発しています。いくつかの画像を読み込もうとすると、OutOfMemoryError が発生します。Androidでこの一般的で頻繁な問題について多くの回答を読みましたが、ロードする画像が非常に大きい場合はすべて対処します。私は自分で画面の回転を管理しているので、回転時に新しいアクティビティを再開することはありません。私はすでにこの質問を読みました:

大きなビットマップ サイズによるメモリ不足例外

Android: ギャラリーでのメモリ不足の例外

画像処理時の Android 処理のメモリ不足例外

私が理解していないことの 1 つは、res フォルダー内のすべての画像 (png ファイル) のサイズを合計すると約 800 kb になることです。しかし、Logcat では、ゲームの作成時 (カード イメージのロード時) のサイズが急速に 20 MB を超えていることに気付きました。私が使用する:このコードは画像をロードします

Resources r = this.getContext().getResources();
CardView.loadCardImages(CardView.EAlone.CARDS, 1027, 615, r.getDrawable(R.drawable.cards));
CardView.loadCardImages(CardView.EAlone.GREEN, mCardWidth, mCardHeight, r.getDrawable(R.drawable.green));
CardView.loadCardImages(CardView.EAlone.RED, mCardWidth, mCardHeight, r.getDrawable(R.drawable.red));
CardView.loadCardImages(CardView.EAlone.WHITE, mCardWidth, mCardHeight, r.getDrawable(R.drawable.white));

CardView.LoadCardImages:

public static void loadCardImages(EAlone mode, int widthPixels, int heightPixels, Drawable tile) {

    Canvas canvas;
    switch (mode)
    {
    case GREEN:
        mAloneGreen = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mAloneGreen);
        break;
    case RED:
        mAloneRed = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mAloneRed);
        break;
    case WHITE:
        mAloneWhite = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mAloneWhite);
        break;
    default:
        mcardImages = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mcardImages);
    }
    tile.setBounds(0, 0, widthPixels, heightPixels);
    tile.draw(canvas);
    tile.setCallback(null);
}

mCardimages には、すべてのカード面が含まれています。次に、このファイルを使用して、必要なカードをファイルから切り取って単一のカードを作成します。この読み込みの後、親ビューに透明度のある他の画像をいくつか読み込みます。

dealerSign = new ImageView(getContext());
dealerSign.setImageResource(R.drawable.dealer);
dealerSign.setAdjustViewBounds(true);
dealerSign.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

setImageResource 行でエラーが発生します。

04-09 22:04:37.226: E/AndroidRuntime(722): FATAL EXCEPTION: main
04-09 22:04:37.226: E/AndroidRuntime(722): java.lang.OutOfMemoryError
04-09 22:04:37.226: E/AndroidRuntime(722):  at    android.graphics.Bitmap.nativeCreate(Native Method)
04-09 22:04:37.226: E/AndroidRuntime(722):  at    android.graphics.Bitmap.createBitmap(Bitmap.java:605)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:767)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.content.res.Resources.loadDrawable(Resources.java:1937)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.content.res.Resources.getDrawable(Resources.java:664)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.widget.ImageView.resolveUri(ImageView.java:537)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.widget.ImageView.setImageResource(ImageView.java:310)
04-09 22:04:37.226: E/AndroidRuntime(722):  at game.spathi.GameView.init(GameView.java:261)
04-09 22:04:37.226: E/AndroidRuntime(722):  at game.spathi.Game.onCreate(Game.java:92)
...

ここで何が間違っていますか?

ありがとうございました!

4

1 に答える 1

3

それを試してみてください -

Bitmap yourBitmap;      
ByteArrayOutputStream baos = new ByteArrayOutputStream();


offerImage.compress(Bitmap.CompressFormat.PNG, 0, baos);
byte[] img = shrinkBitmap(baos.toByteArray());

//convert this byte image to bitmap and load it...

private byte[] shrinkBitmap(byte[] data){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; //try to decrease decoded image
    options.inPurgeable = true; //purgeable to disk
    Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(data, 0, data.length, options), 100, 100, true);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 20, buf);

    return(buf.toByteArray());
}
于 2012-04-10T09:26:16.127 に答える