1

I am facing this problem: OutOfMemoryError!!

Here is the screen that causes the problem:

enter image description here

When I using mouse to scroll down. My app suddenly shutdown, and here is what logcat shows:

09-05 08:25:36.469: E/AndroidRuntime(8941): FATAL EXCEPTION: AsyncTask #5
09-05 08:25:36.469: E/AndroidRuntime(8941): java.lang.RuntimeException: An error occured while executing doInBackground()
09-05 08:25:36.469: E/AndroidRuntime(8941):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.lang.Thread.run(Thread.java:841)
09-05 08:25:36.469: E/AndroidRuntime(8941): Caused by: java.lang.OutOfMemoryError
09-05 08:25:36.469: E/AndroidRuntime(8941):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at com.ptit.piclient.tools.AllSongAdapter2$DownloadImageTask.doInBackground(AllSongAdapter2.java:91)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at com.ptit.piclient.tools.AllSongAdapter2$DownloadImageTask.doInBackground(AllSongAdapter2.java:1)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-05 08:25:36.469: E/AndroidRuntime(8941):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-05 08:25:36.469: E/AndroidRuntime(8941):     ... 4 more

It says that problem is the DownloadImageTask class. And here is the code of that class:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{
        ImageView imgView;
        public DownloadImageTask(ImageView img){this.imgView = img;}

        @Override
        protected Bitmap doInBackground(String... urls) {
            String urlDisplay = urls[0];
            Bitmap icon = null;
            InputStream in = null ;
            try {
                in = new URL(urlDisplay).openStream();
                icon = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //khuongdv start sep28
            finally{
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            //khuongdv end sep28
            return icon;
        }
        protected void onPostExecute(Bitmap result){
            if(result == null) return;
            imgView.setImageBitmap(result);
        }
    }

Can you point out why is this and how to fix this!?

Thanks!

Songokute
  • 687
  • 1
  • 9
  • 17
  • You have to handle the bitmap size, it is causing the out of memory error. Please refer to this: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 Please also consider lazy loading of listview when doing so from a webservice. – Skynet Sep 28 '13 at 06:34
  • Can you tell me more details? – Songokute Sep 28 '13 at 06:34
  • You are trying to load the full scale images. Since there are a list of them it may cause out of memory. follow this: http://stackoverflow.com/a/823966/827110 – Amulya Khare Sep 28 '13 at 06:35
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – yanchenko Sep 28 '13 at 06:35
  • icon = Bitmap.createScaledBitmap(icon,64,64,false); after icon = BitmapFactory.decodeStream(in); try that – JRowan Sep 28 '13 at 06:40
  • @Songokute-Just add external storage permission in manifest.xml file sometimes this is the reason to showing that type of error – FarhaSameer786 Sep 28 '13 at 06:43
  • @FarhaSameer786: I did add two permissions about storage (WRITE and READ EXTERNAL STORAGE) :( @ JRowan: Thanks , it works. No error occurs now :D @ Amulya Khare and @ Pirate Cube and yanchenko: Thank you all ^_^ – Songokute Sep 28 '13 at 06:53

2 Answers2

1

Use the imageloader from the lazylist which is in following link it will help you to load image from url and meanwhile you can show temp image. and it also provide file caching so next time you don't need to download that image again.

LazyList

use the following code wherever you want to load image.

ImageLoader imageLoader=new ImageLoader(this); imageLoader.displayImage(url,imageview);

and add user permission to write external storage b'coze of it's use file caching.

Hope you findout it useful...

Community
  • 1
  • 1
sachin
  • 29
  • 4
0

I don't think a lazy list goes far enough to resolve this problem. It is if you're loading from disk but otherwise it's not because a single image might be sufficient to take all your memory.

BitmapFactory has methods to decode only the header so that you do a second pass decode that will downscale the image to avoid this problem. However, it's much simpler to use something like Picasso and let it handle this for you.

pjulien
  • 1,369
  • 10
  • 14