简体   繁体   中英

java.lang.OutOfMemoryError: bitmap size exceeds VM budget in listview

I was trying the hp.android answer in the https://stackoverflow.com/a/6779067/1236259 thread but my activity never invokes the onDestroy method

I have a ListView with images and I call to other activity by:

ImageButton imageButtonSeeMap = (ImageButton)this.findViewById(R.id.imageButtonSeeMap);
imageButtonSeeMap.setOnClickListener(new OnClickListener() {....

From the new activity I invoke the ListView activity again and in this point I´m getting the exeption: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

The image in the listview is created using:

thumb_u = new URL(lp.get(i).getImage());
            thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");

            imageViewPoi.setImageDrawable(thumb_d);

How can I release the memory of the images? The unbindDrawables in the onDestroy method is never invoked.

Any idea?

Images are beast in android, so OutofMemory exceptions are kinda something you might have to get used to. This should allow your listview to get images from the web and not crash yer app.

Assuming that the 2nd code block is from your ListView's adapter getView() method, try :

try {
    thumb_u = new URL(lp.get(i).getImage());
    thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    imageViewPoi.setImageDrawable(thumb_d);
} catch (OutOfMemoryError e) {
    System.gc();
    thumb_u = new URL(lp.get(i).getImage());
    thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    imageViewPoi.setImageDrawable(thumb_d);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM