android - Load applications icons and getting OutOfMemory exception while resizing -


here code : (all executed async while showing progress bar)

list<resolveinfo> apps = pm.queryintentactivities(intent, packagemanager.get_meta_data); (resolveinfo app : apps) {     string label = app.activityinfo.loadlabel(pm).tostring();     drawable icon = app.activityinfo.loadicon(pm);     drawable resizedicon = null;     if (icon instanceof bitmapdrawable) {         resizedicon = graphics.resize(icon, res, iconw, iconh);     }     appinfo ai = new appinfo(app, label, resizedicon);     items.add(ai); } 

here graphics.resize() :

bitmap b = ((bitmapdrawable)image).getbitmap(); bitmap bitmapresized = bitmap.createscaledbitmap(b, widthpx, heightpx, false); b.recycle(); return new bitmapdrawable(res, bitmapresized); 

generally works fine, did report out of memory exception while calling bitmap.createscaledbitmap (i'm trying re-size 32dp)
read handling , displaying bitmaps , here, want app icon displayed while user sees app name, , not start being loaded. (which can achieve using recyclerview)

when system can't satisfy needs of application specially resource error can see happening running low on memory

you ca increase amount of heap memory provided application (process) adding attribute application tag in androidmanifest.xml

android:largeheap="true" 

your application tag this...

<application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:supportsrtl="true"     android:largeheap="true"     android:theme="@style/apptheme"> 

although increase memory provided application (process). not guarantee enough task want perform.

if trying start memory expensive task, add of code

    system.gc(); 

this may free memory on heap. depends on dvm ( dalivik virtual machine) deal heap.

make sure call before starting heavy task otherwise useless calling after task has been completed.

if facing issue on emulator can try increasing heap memory of avd (android virtual device)

avd edit

when dealing bitmaps images try them in small possible common culprit outofmemory error.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -