Justin D
Justin D

Reputation: 697

Android app OutOfMemoryError while decoding bitmap stream

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!

Upvotes: 1

Views: 541

Answers (2)

pjulien
pjulien

Reputation: 1379

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.

Upvotes: 0

sachin
sachin

Reputation: 29

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

Upvotes: 1

Related Questions