【问题标题】:Android OOM Error, Loading multiple imagesAndroid OOM 错误,加载多个图像
【发布时间】:2018-08-16 04:10:53
【问题描述】:

我是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;
    }

}

【问题讨论】:

  • 使用 Picasso 或 Fresco 并为自己节省数学和计算

标签: java android memory-management


【解决方案1】:

所以我一开始就有 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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多