2

This is what I am doing, Firstly into the constructor of Customadapter, I have initilize image cache and image loader

imageCache = new BitmapLruCache();
mImageLoader =  new ImageLoader(newRequestQueue(context), imageCache);

then into the getView() method I have done...

String url = imgs[position];
image.setImageUrl(url, mImageLoader);
image.setDefaultImageResId(R.drawable.no_image);
image.setErrorImageResId(R.drawable.error_image);

Where imgs is the array for bitmaps url and image is the object of NetworkImageView. When I execute it, I got error of OutOfMemory. This is my logcat o/p :-

    02-24 12:08:08.845: E/dalvikvm-heap(6288): Out of memory on a 144636048-byte allocation.
02-24 12:08:08.845: I/dalvikvm(6288): "main" prio=5 tid=1 RUNNABLE
02-24 12:08:08.845: I/dalvikvm(6288):   | group="main" sCount=0 dsCount=0 obj=0x41767b50 self=0x417571d0
02-24 12:08:08.845: I/dalvikvm(6288):   | sysTid=6288 nice=0 sched=0/0 cgrp=apps handle=1075191004
02-24 12:08:08.845: I/dalvikvm(6288):   | state=R schedstat=( 0 0 0 ) utm=116 stm=35 core=1
02-24 12:08:08.845: I/dalvikvm(6288):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:815)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.content.res.Resources.loadDrawable(Resources.java:1992)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.content.res.Resources.getDrawable(Resources.java:666)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.widget.ImageView.resolveUri(ImageView.java:616)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.widget.ImageView.setImageResource(ImageView.java:349)
02-24 12:08:08.855: I/dalvikvm(6288):   at com.android.volley.toolbox.NetworkImageView$1.onResponse(NetworkImageView.java:177)
02-24 12:08:08.855: I/dalvikvm(6288):   at com.android.volley.toolbox.NetworkImageView$1$1.run(NetworkImageView.java:168)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.os.Handler.handleCallback(Handler.java:725)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.os.Looper.loop(Looper.java:137)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.app.ActivityThread.main(ActivityThread.java:5099)
02-24 12:08:08.855: I/dalvikvm(6288):   at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:08:08.865: I/dalvikvm(6288):   at java.lang.reflect.Method.invoke(Method.java:511)
02-24 12:08:08.865: I/dalvikvm(6288):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
02-24 12:08:08.865: I/dalvikvm(6288):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
02-24 12:08:08.865: I/dalvikvm(6288):   at dalvik.system.NativeStart.main(Native Method)
02-24 12:08:08.865: D/skia(6288): --- decoder->decode returned false
02-24 12:08:08.865: D/AndroidRuntime(6288): Shutting down VM
02-24 12:08:08.865: W/dalvikvm(6288): threadid=1: thread exiting with uncaught exception (group=0x41766ae0)
02-24 12:08:08.875: E/AndroidRuntime(6288): FATAL EXCEPTION: main
02-24 12:08:08.875: E/AndroidRuntime(6288): java.lang.OutOfMemoryError
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:815)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.content.res.Resources.loadDrawable(Resources.java:1992)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.content.res.Resources.getDrawable(Resources.java:666)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.widget.ImageView.resolveUri(ImageView.java:616)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.widget.ImageView.setImageResource(ImageView.java:349)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.volley.toolbox.NetworkImageView$1.onResponse(NetworkImageView.java:177)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.volley.toolbox.NetworkImageView$1$1.run(NetworkImageView.java:168)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.os.Handler.handleCallback(Handler.java:725)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.os.Looper.loop(Looper.java:137)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.app.ActivityThread.main(ActivityThread.java:5099)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at java.lang.reflect.Method.invoke(Method.java:511)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at dalvik.system.NativeStart.main(Native Method)

I cant find any solution that will work for me, So What should I do...

Rohit
  • 687
  • 5
  • 22

2 Answers2

1

Thanks to Mr. Itai Hanski,

After your suggestion about OOM that OOM doesn't have to do with the network components, I had looked out on any other cause, Finally I found solution :

It was from layout file of listview item, I had set the android:src="" into the Imageview. When I remove this xml attribute from ImageView, The OOM problem resolve out.

But one thing I still not found that when I set a image for error or loading condition the error again comes out, still I cant uderstand what the real cause of it.

Rohit
  • 687
  • 5
  • 22
0

Bitmaps are a real issue in android.

First off - I really recommend you read this post on the developer site.

How is your BitmapLruCache implemented? Here are a few pointers (you can use only some of them):

  1. Limit the number of bitmaps the cache can hold.
  2. Limit the size of the cache itself. The Android guys provide a nice algorithm to decide how large to make it in the linked article.
  3. When possible, call bitmap.recycle().
  4. Use weak / soft references to the Bitmaps in your cache instead of strong one.
  5. Check out this SO answer to a similar question (shameless self plug, but relevant).

You probably don't need to implement all of these at the same time. Choose the ones that work for you.

Community
  • 1
  • 1
Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
  • I cant understand the things, I have used volley, UniversalImageLoader, UrlImageViewHelper, diskLruCache and ImageLoader also, all of these are give out OutOfMemoryError. Could you please explain me from where I again start, Which is better approach, I want to show images in gridView adapter. – Rohit Feb 26 '14 at 11:01
  • 1
    OOM doesn't have to do with the network component, it happens when memory management fails and the amount of memory you require exceeds the limit on the device and OS you're running. Read the post I linked. – Itai Hanski Feb 26 '14 at 11:32