android java.lang.OutOfMemoryError: bitmap size exceeds VM budget

纵饮孤独 提交于 2019-12-11 10:58:18

问题


i've some leaks problems. What i'm doing is loading some pictures in a table. I've created an asyncronous class for loading images.

In my table while i'm cycling the array I add my class

final LoaderImageView image = new LoaderImageView(getContext(),vgood[i].getImageUrl(),(int)(ratio *80),(int)(ratio *80));
row.addView(image);

In my asyncronous class i'm loading images from this

return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), "name");

Called by

public void setImageDrawable(final String imageUrl) {
    mDrawable = null;
    mSpinner.setVisibility(View.VISIBLE);
    mImage.setVisibility(View.GONE);
    new Thread(){
        public void run() {
            try {
                mDrawable = getDrawableFromUrl(imageUrl);
                imageLoadedHandler.sendEmptyMessage(COMPLETE);
            } catch (MalformedURLException e) {
                imageLoadedHandler.sendEmptyMessage(FAILED);
                e.printStackTrace();
            } catch (IOException e) {
                imageLoadedHandler.sendEmptyMessage(FAILED);
                e.printStackTrace();
            }
        };
    }.start();
}

When i've a lot of images to load the app crashes due to out of memory caused by this line

return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), "name");

How can i fix that? where i'm going wrong?

thank you


回答1:


  1. Never load any tiles that you don't need. "am collecting all bitmaps from sdcard" suggests that you are decompressing every image you might ever need, which would be the wrong approach.

  2. Use smaller tiles. 256x256 is a good size.

  3. Load them as Config.RGB565 (i.e. 16bpp).

  4. Regularly recycle() Bitmaps you don't need.

Basically you need to implement some sort of most-recently-used (MRU) Bitmap cache

Extrated From

Android: "java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android" While loading bitmaps from sdcard?



来源:https://stackoverflow.com/questions/5584534/android-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!