位图德codeResource - 三星S5碰撞出内存不足 [英] Bitmap decodeResource - out-of-memory crash on Samsung S5

查看:153
本文介绍了位图德codeResource - 三星S5碰撞出内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展现的背景图像时,用银河S5每次面临崩溃。

此背景位于xxhdpi资源文件夹,大小是一样的S5屏幕(1080x1920),所以我不需要叫createScaledBitmap缩放它。该图像的分辨率为96DPI JPG

和调用去codeResource时...崩溃!这怎么可能?是我加载在这个超级强大的设备的唯一位图。

谢谢!

下面我code(比例= 1 S5):

 公共静态位图德codeBitmapFromResource(资源RES,INT渣油,浮动比例){    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeResource(RES,渣油,期权);    options.inSampleSize = calculateInSampleSize(选项,
            (中间体)(options.outWidth *刻度),
            (中间体)(options.outHeight *刻度));    options.inJustDe codeBounds = FALSE;    如果(规模大于1){
        位图位图= BitmapFactory.de codeResource(RES,渣油);
        返回Bitmap.createScaledBitmap(位图,(int)的(options.outWidth *刻度),
                (中间体)(options.outHeight *刻度),真);
    }    返回BitmapFactory.de codeResource(RES,渣油,期权);
}


解决方案

在您的清单文件添加这一行中的应用程序标签。它并没有解决问题只是让你的应用程序有更多的内存:

 的android:largeHeap =真

更新:

不过使用 largeHeap 不是一个很好的解决方案。这里是谷歌的这个文档。


  

不过,要求一大堆的能力,仅用于一
  小集合的应用程序,可以证明需要消耗更多的内存(如
  作为一个大的照片编辑应用程序)。 从不简单地要求一大堆
  因为你已经用完了内存,你需要一个快速解决 - 你应该
  使用它,只有当你确切地知道,所有的内存被
  分配以及为什么必须保留。然而,即使你有信心
  您的应用程序可以证明一大堆,你应该避免请求
  无论尽可能。利用额外的内存将越来越多地
  到整体用户体验,因为垃圾造成损害
  收集将需要更长的时间和系统性能可能会较慢时,
  任务切换或执行其他常见操作。


和有关加载位图:


  

当你加载一个位图,保存在RAM只在您需要的分辨率
  当前设备的屏幕,比例缩小,如果原来的
  位图是一个更高的分辨率。请记住,增加的位图
  在相应的(increase2)在内存解析结果需要,
  因为无论是X和Y尺寸增加


这是不差地看看这个页面,它解释了内存管理方式:

如何您的应用应当管理存储器

所以我觉得我最后的答案是不是一个很好的解决方案,你可能会重新考虑自己的战略中加载图像。希望这个答案可以帮助你;)

I'm facing a crash every time with a Galaxy S5 when trying to show a background image.

This background is located in xxhdpi resource folder, the size is the same as the S5 screen (1080x1920) so I don't need to call "createScaledBitmap" for scaling it. The resolution of this image is JPG 96dpi.

And when calling decodeResource... crash!!! How is this possible? Is the only bitmap I'm loading in this "super-powerful" device.

Thanks!!!

Below my code (scale = 1 for S5):

public static Bitmap decodeBitmapFromResource(Resources res, int resId, float scale) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    options.inSampleSize = calculateInSampleSize(options, 
            (int)(options.outWidth*scale), 
            (int)(options.outHeight*scale));

    options.inJustDecodeBounds = false;

    if (scale > 1) {
        Bitmap bitmap = BitmapFactory.decodeResource(res, resId);
        return Bitmap.createScaledBitmap(bitmap, (int)(options.outWidth*scale), 
                (int)(options.outHeight*scale), true);
    }

    return BitmapFactory.decodeResource(res, resId, options);
}

解决方案

Add this line in your Manifest file in the application tag. It doesn't solve the problem just allows your app to have more memory:

 android:largeHeap="true"

UPDATE:

However using largeHeap is not a good solution. here is the google's doc about this.

However, the ability to request a large heap is intended only for a small set of apps that can justify the need to consume more RAM (such as a large photo editing app). Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained. Yet, even when you're confident your app can justify the large heap, you should avoid requesting it to whatever extent possible. Using the extra memory will increasingly be to the detriment of the overall user experience because garbage collection will take longer and system performance may be slower when task switching or performing other common operations.

And about loading bitmaps:

When you load a bitmap, keep it in RAM only at the resolution you need for the current device's screen, scaling it down if the original bitmap is a higher resolution. Keep in mind that an increase in bitmap resolution results in a corresponding (increase2) in memory needed, because both the X and Y dimensions increase.

It's not bad to take a look at this page, it explains ways of managing memory:

How Your App Should Manage Memory

So I think my last answer is not a good solution and You might rethink your strategy in loading images. Hope this answer helps you ;)

这篇关于位图德codeResource - 三星S5碰撞出内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆