0

I am using Volley ImageLoader to load images from server and display theme inside my RecyclerView, here is code of my ImageLoader:

 mImageLoader=new ImageLoader(mRequestQueue,new ImageLoader.ImageCache() {

        private LruCache<String, Bitmap> cache=new LruCache<>((int)(Runtime.getRuntime().maxMemory()/1024)/8);
        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            cache.put(url, bitmap);
        }
    });

Im loading photos in my Adapter:

if (currentItem.myPlikPhotoUrl != null){

        imageLoader.get(
            currentItem.myPlikPhotoUrl,
            new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                holder.myPlikPhoto.setImageBitmap(imageContainer.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

Everything works Ok, but when i am scrolling too fast i get OOM exceptions, there is some large images, so it may be hard to work with, how should i work with this kind of issues, and what is the right way of handling image loading?

Logcat error:

09-29 17:53:48.024    4629-4657/com.plikster.plikster I/art﹕ Clamp target GC heap from 66MB to 64MB
09-29 17:53:48.024    4629-4657/com.plikster.plikster I/art﹕ HomogeneousSpaceCompact marksweep + semispace GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 2% free, 62MB/64MB, paused 8.700ms total 8.700ms
09-29 17:53:48.024    4629-4657/com.plikster.plikster W/art﹕ Throwing      OutOfMemoryError "Failed to allocate a 4283669 byte allocation with 1545568 free bytes and 1509KB until OOM"
09-29 17:53:48.033    4629-4658/com.plikster.plikster I/art﹕ Starting a blocking GC Alloc
09-29 17:53:48.033    4629-4658/com.plikster.plikster I/art﹕ Starting a blocking GC Alloc
09-29 17:53:48.035    4629-4657/com.plikster.plikster E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-465
Process: com.plikster.plikster, PID: 4629
java.lang.OutOfMemoryError: Failed to allocate a 4283669 byte allocation with 1545568 free bytes and 1509KB until OOM
        at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
        at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:257)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:130)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
09-29 17:53:48.043    4629-4658/com.plikster.plikster I/art﹕ Alloc sticky concurrent mark sweep GC freed 57(9KB) AllocSpace objects, 0(0B) LOS objects, 2% free, 62MB/64MB, paused 1.877ms total 8.844ms
09-29 17:53:48.044    4629-4658/com.plikster.plikster I/art﹕ Starting a blocking GC Alloc
09-29 17:53:48.119    4629-4660/com.plikster.plikster W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-29 17:53:48.119    4629-4660/com.plikster.plikster W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9febd780, error=EGL_SUCCESS
09-29 17:53:48.265    4629-4658/com.plikster.plikster I/art﹕ Alloc partial concurrent mark sweep GC freed 149(9KB) AllocSpace objects, 1(4MB) LOS objects, 6% free, 58MB/62MB, paused 50.522ms total 213.856ms
09-29 17:53:49.629    4629-4660/com.plikster.plikster E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xb4017a20

There was other errors befor i changed ImageView to NetworkImageView...

3 Answers3

1

Since you are already using volley, you can painlessly switch to NetworkImageView from ImageView and probably avoid these issues.

You can use ImageLoader and NetworkImageView in concert to efficiently manage the display of multiple images, such as in a ListView. In your layout XML file, you use NetworkImageView in much the same way you would use ImageView

The same page provides a sample code for an LRU cache which hopefully you will find useful.

UPDATE: If you continue to have issues with NetworkImageView you might need to switch to a Disk based cache implementation. A very popular disk cache is DiskLruCache by Jake Warton of ActionbarSherlock fame. Luckily I discovered a blog post which explains how the two have been integrated.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Hmm, it dont helped, but i have some thought about it, may it be that there is some too big image, that cant be saved in cache or is there some way to handle image loading without cache? – Zoltan Kurtyak Sep 29 '15 at 14:26
  • i dont khow how, can you help me with this? – Zoltan Kurtyak Sep 29 '15 at 14:33
  • stacktrace as in the exception related error messages that show up in the logcat. If you are using Eclipse, it's pretty hard to copy from the eclipse logcat window but you can run logcat on command line and grab it from there. Android studio logcat is in the tab titled android on the bottom statusbar – e4c5 Sep 29 '15 at 14:40
0

I recommend to use Picasso instead of this, this handles your cache and network operations automatically.

Akber
  • 521
  • 4
  • 10
0

Reduce the size of your cache.

...a cache that is too large can once again cause java.lang.OutOfMemory exceptions...

https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Freek Nortier
  • 780
  • 1
  • 13
  • 27