Getting a lot of App Icons makes Out Of Memory

2020-03-01 01:25发布

I created an app to load all installed applications. It's working fine now. But with some phones/tablets that have a lot of applications, my app crashes because of Out Of Memory. Below is the source code to get application info

String appName = p.applicationInfo.loadLabel(packageManager).toString();
                    String packageName = p.packageName;
                    String appFile = a.sourceDir;
                    long lastUpdated = new File(appFile).lastModified();
                    Drawable icon = a.loadIcon(packageManager);
                    AppSize appSize = calculateSize(p.applicationInfo.sourceDir);
                    App application = new App(appName, packageName, lastUpdated, appSize.getSize(), appSize.getLongSize(), icon, false);

Is there any way to optimize above code to avoid out of memory? Thank you.

1条回答
▲ chillily
2楼-- · 2020-03-01 01:54

Please have look at this link: Displaying bitmaps efficiently.

Also at this open source library project: Universal Image loader.

when your application needs to display lots of bitmaps, it exceeds allocated memory of application. Also, when they are displayed in grid,list or view pager more memory is consumed while creating each views. hence you need to cache in those images in memory or disc and display them efficiently using the techniques described in those links.

Addition

Take a look at this: Get installed Applications with Name, Package Name, Version and Icon

Acceptable URI's Examples for UIL:

String imageUri = "http://site.com/image.png"; // from Web
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
查看更多
登录 后发表回答