内存溢出应对:把图片固定大小赋值给bitmap



主题:
//将绝对路径转换为Bitmap,动态计算inSampleSize限制图片大小此处大小为128*128 RGB_565(默认为8888)下为 2.7K
    public static Bitmap getFixedSizeImg(String filePath){
        Bitmap bitmap = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        BitmapFactory.decodeFile(filePath, opts);

        opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
        opts.inJustDecodeBounds = false;

        try {
            bitmap = BitmapFactory.decodeFile(filePath, opts);
        }catch (Exception e) {
            // TODO: handle exception
        }
        return bitmap;
    }
//计算inSampleSize
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }
//计算inSampleSize
    private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
主题结束
重写Activity此方法可查看app内存状态
@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    switch (level) {
        case TRIM_MEMORY_RUNNING_MODERATE:
            LongLog.loge("你的app正在运行并且不会被列为可杀死的");
            break;
        case TRIM_MEMORY_RUNNING_LOW:
            LongLog.loge("你的app正在运行且没有被列为可杀死的");
            break;
        case TRIM_MEMORY_RUNNING_CRITICAL:
            LongLog.loge("你的app仍在运行,但是系统已经把LRU Cache中的大多数进程都已经杀死");
            break;
        case TRIM_MEMORY_BACKGROUND:
            LongLog.loge("系统正运行于低内存");
            break;
        case TRIM_MEMORY_MODERATE:
            LongLog.loge(" 系统正运行于低内存状态并且你的进程已经已经接近LRU名单的中部位置");
            break;
        case TRIM_MEMORY_COMPLETE:
            LongLog.loge("系统正运行与低内存的状态并且你的进程正处于LRU名单中最容易被杀掉的位置");
            break;
    }
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将一个 Bitmap 对象赋值给另一个 Bitmap 对象,可以使用 `Bitmap.copy()` 方法。这个方法会创建一个新的 Bitmap 对象,并将原始 Bitmap 对象的图像数据复制到新的 Bitmap 对象中。然后,可以回收原始的 Bitmap 对象占用的内存。 以下是一个示例代码: ``` Bitmap originalBitmap = ...; // 原始的 Bitmap 对象 Bitmap newBitmap = originalBitmap.copy(originalBitmap.getConfig(), true); // 创建新的 Bitmap 对象,并复制原始 Bitmap 对象的图像数据 originalBitmap.recycle(); // 回收原始 Bitmap 对象占用的内存 originalBitmap = null; // 将变量置为 null,方便垃圾回收 ``` 需要注意的是,在使用 `Bitmap.copy()` 方法创建新的 Bitmap 对象时,需要指定 Bitmap.Config 参数和 boolean 参数。其中,Bitmap.Config 参数指定新的 Bitmap 对象的像素格式,boolean 参数指定新的 Bitmap 对象是否应该是可变的(即是否可以修改图像数据)。如果将 boolean 参数设置为 true,新的 Bitmap 对象就是可变的。 在回收原始的 Bitmap 对象占用的内存时,可以使用 `Bitmap.recycle()` 方法。但是,如果新的 Bitmap 对象是可变的,就不能在回收原始的 Bitmap 对象之前回收新的 Bitmap 对象占用的内存。否则,新的 Bitmap 对象的图像数据就会丢失。 因此,在将一个 Bitmap 对象赋值给另一个 Bitmap 对象并回收原始的 Bitmap 对象占用的内存时,需要先检查新的 Bitmap 对象是否是可变的,然后再决定是否需要回收它占用的内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值