显示与所有应用程序对话的android outOfMemoryError

时间:2013-11-11 16:59:26

标签: java android bitmap out-of-memory drawable

我在显示一个显示所有已安装应用的对话框时得到了这个

E/AndroidRuntime( 1148): java.lang.OutOfMemoryError
E/AndroidRuntime( 1148):        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/AndroidRuntime( 1148):        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
E/AndroidRuntime( 1148):        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
E/AndroidRuntime( 1148):        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
E/AndroidRuntime( 1148):        at android.content.res.Resources.loadDrawable(Resources.java:2988)
E/AndroidRuntime( 1148):        at android.content.res.Resources.getDrawable(Resources.java:1558)
E/AndroidRuntime( 1148):        at android.app.ApplicationPackageManager.getDrawable(ApplicationPackageManager.java:712)
E/AndroidRuntime( 1148):        at android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:149)
E/AndroidRuntime( 1148):        at android.content.pm.ComponentInfo.loadDefaultIcon(ComponentInfo.java:167)
E/AndroidRuntime( 1148):        at android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:154)
E/AndroidRuntime( 1148):        at android.content.pm.ResolveInfo.loadIcon(ResolveInfo.java:226)

在对话框中,我正在List ResolveInfo进行迭代,然后通过以下方式加载应用图标:

Drawable app_icon = allappslist.get(i).loadIcon(context.getPackageManager()));

但这有时会产生上述错误......

2 个答案:

答案 0 :(得分:0)

我在Crashes & ANRs报告中从Google Developer Console得到了这个问题: java.lang.OutOfMemoryError: Failed to allocate a 1048588 byte allocation with 250994 free bytes and 245KB until OOM

无论如何,由于我的应用程序也获得了所有已安装的应用程序,我已经做了很多事情。

1-使用LruCache类来缓存所有位图,你也可以使用这个类,我在StackOverFlow的答案中找到了这个类:

import android.support.v4.util.LruCache;

//This class to cache bitmap apps icon
public class Cache {

    private static Cache instance;
    private LruCache<Object, Object> lru;

    //------------------------------------------------------------------------//
    private Cache() {
        lru = new LruCache<>(5 * 1024 * 1024) //Max is 5MB;
    }
    //------------------------------------------------------------------------//
    public static Cache getInstance() {
        if (instance == null) {
            instance = new Cache();
        }
        return instance;
    }
    //------------------------------------------------------------------------//
    public LruCache<Object, Object> getLru() {
        return lru;
    }
}

2-使用Bitmap.createScaledBitmap缩小应用图标尺寸,这会减小尺寸more details

用于检查缓存并获取位图的代码片段:

Object appBitmap = Cache.getInstance().getLru().get(this.packageName);
if(appBitmap == null){
    Drawable drawableAppIcon = packageInfo.applicationInfo.loadIcon(packageManager);
    Bitmap bitmap = ((BitmapDrawable)drawableAppIcon).getBitmap();
    this.appIcon = Bitmap.createScaledBitmap(bitmap, 40,40,true);
    Cache.getInstance().getLru().put(this.packageName, this.appIcon);
} else{
    this.appIcon = (Bitmap)appBitmap;
}

祝你好运!

答案 1 :(得分:-1)

您可以阅读本教程。这真的很有帮助。

http://developer.android.com/training/displaying-bitmaps/index.html

相关问题