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!

7
  • You have to handle the bitmap size, it is causing the out of memory error. Please refer to this: stackoverflow.com/questions/477572/… Please also consider lazy loading of listview when doing so from a webservice. Commented Sep 28, 2013 at 6:34
  • Can you tell me more details? Commented Sep 28, 2013 at 6: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: stackoverflow.com/a/823966/827110 Commented Sep 28, 2013 at 6:35
  • possible duplicate of Strange out of memory issue while loading an image to a Bitmap object Commented Sep 28, 2013 at 6:35
  • icon = Bitmap.createScaledBitmap(icon,64,64,false); after icon = BitmapFactory.decodeStream(in); try that Commented Sep 28, 2013 at 6:40

2 Answers 2

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...

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.