Android OOM错误,加载多个图像

时间:2018-03-07 22:39:00

标签: java android memory-management

我是looking at this answer over here,但它并没有真正解决我的问题。

由于某些原因,即使我删除了manifest.xml文件夹,build也不会覆盖我的更改。

我试图在屏幕上加载5张图片,但即使经过优化也是如此;他们仍然没有记忆。

我正在看video that teaches image scaling。它确实解决了我的一小部分问题。我能够加载我的背景图像,但是我无法加载我的其他4张图像。

如何更有效地进行优化?我正在观看此视频关于Bitmap optimization by Android,但我对此并不了解。

这是我的源代码

MainActivity.java

package com.example.cliente.myapplication;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private static final float BYTES_PER_PX = 4.0f;
    ImageView backgroundImage, upImage, downImage, leftImage, alertImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        backgroundImage = findViewById(R.id.background_image);
        loadImage(backgroundImage, R.drawable.background);

        upImage = findViewById(R.id.up_arrow_image);
        loadImage(upImage, R.drawable.up);

        downImage = findViewById(R.id.down_arrow_image);
        loadImage(downImage, R.drawable.down);

        leftImage = findViewById(R.id.left_arrow_image);
        loadImage(leftImage, R.drawable.left);

        alertImage = findViewById(R.id.alert_image);
        loadImage(alertImage, R.drawable.alert);

    }

    private void loadImage(ImageView image, int imgSrc) {
        if (readBitmapInfo() > MemUtils.megabytesFree()) {
            subSampleImage(image, 32);
        } else {
            image.setImageResource(imgSrc);
        }
    }

    private float readBitmapInfo() {
        final Resources res = this.getResources();
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, R.drawable.background, options);
        final float imageHeight = options.outHeight;
        final float imageWidth = options.outWidth;

        return imageWidth * imageHeight * BYTES_PER_PX / MemUtils.BYTES_IN_MB;
    }

    private void subSampleImage(ImageView image, int powerOf2) {
        if (powerOf2 < 1 || powerOf2 > 2) {
            return;
        }

        final Resources res = this.getResources();
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = powerOf2;
        final Bitmap map = BitmapFactory.decodeResource(res, R.drawable.background, options);
        image.setImageBitmap(map);
    }

}

MemUtils.java

package com.example.cliente.myapplication;

public class MemUtils {
    public static final float BYTES_IN_MB = 1024.0f * 1024.0f;

    public static float megabytesFree() {
        final Runtime rt = Runtime.getRuntime();
        final float bytesUsed = rt.totalMemory();
        final float mbUsed = bytesUsed / BYTES_IN_MB;
        final float mbFree = megabytesAvailable() - mbUsed;
        return mbFree;
    }

    public static float megabytesAvailable() {
        final Runtime rt = Runtime.getRuntime();
        final float bytesAvailable = rt.maxMemory();
        return bytesAvailable / BYTES_IN_MB;
    }

}

1 个答案:

答案 0 :(得分:0)

所以我开始时有 2个错误

  • OOM(内存不足)异常,非常感谢@Chisko,我使用了一个名为Picasso的库来解决我的问题。
  • 另一个问题是Canvas: trying to draw too large...问题。我需要将图片从drawable-mdpi移至drawable-xxhdpi

以下是我的问题的解决方案:

package com.example.cliente.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    ImageView backgroundImage, upImage, downImage, leftImage, alertImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        backgroundImage = findViewById(R.id.background_image);
        Picasso.get().load(R.drawable.background).into(backgroundImage);

        upImage = findViewById(R.id.up_arrow_image);
        Picasso.get().load(R.drawable.up).into(upImage);

        downImage = findViewById(R.id.down_arrow_image);
        Picasso.get().load(R.drawable.down).into(downImage);

        leftImage = findViewById(R.id.left_arrow_image);
        Picasso.get().load(R.drawable.left).into(leftImage);

        alertImage = findViewById(R.id.alert_image);
        Picasso.get().load(R.drawable.alert).into(alertImage);
    }
}