Using disk cache with Volley and ListView is very slow

I am trying to use this library https://github.com/JakeWharton/DiskLruCache for disk cache, but I am having problems when I have a lot of images in my View List. Sometimes the lag is really bad.

I used the following approach, for example from this GitHub repository: https://github.com/rdrobinson3/VolleyImageCacheExample

public DiskLruImageCache(Context context,String uniqueName, int diskCacheSize,
    CompressFormat compressFormat, int quality ) {
    try {
            final File diskCacheDir = getDiskCacheDir(context, uniqueName );
            mDiskCache = DiskLruCache.open( diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize );
            mCompressFormat = compressFormat;
            mCompressQuality = quality;
        } catch (IOException e) {
            e.printStackTrace();
        }
}

private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor )
    throws IOException, FileNotFoundException {
    OutputStream out = null;
    try {
        out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
        return bitmap.compress( mCompressFormat, mCompressQuality, out );
    } finally {
        if ( out != null ) {
            out.close();
        }
    }
}

private File getDiskCacheDir(Context context, String uniqueName) {

    final String cachePath = context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}

@Override
public void putBitmap( String key, Bitmap data ) {

    DiskLruCache.Editor editor = null;
    try {
        editor = mDiskCache.edit( key );
        if ( editor == null ) {
            return;
        }

        if( writeBitmapToFile( data, editor ) ) {               
            mDiskCache.flush();
            editor.commit();
            if ( BuildConfig.DEBUG ) {
               Log.d( "cache_test_DISK_", "image put on disk cache " + key );
            }
        } else {
            editor.abort();
            if ( BuildConfig.DEBUG ) {
                Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
            }
        }   
    } catch (IOException e) {
        if ( BuildConfig.DEBUG ) {
            Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
        }
        try {
            if ( editor != null ) {
                editor.abort();
            }
        } catch (IOException ignored) {
        }           
    }

}

@Override
public Bitmap getBitmap( String key ) {

    Bitmap bitmap = null;
    DiskLruCache.Snapshot snapshot = null;
    try {

        snapshot = mDiskCache.get( key );
        if ( snapshot == null ) {
            return null;
        }
        final InputStream in = snapshot.getInputStream( 0 );
        if ( in != null ) {
            final BufferedInputStream buffIn = 
            new BufferedInputStream( in, IO_BUFFER_SIZE );
            bitmap = BitmapFactory.decodeStream( buffIn );              
        }   
    } catch ( IOException e ) {
        e.printStackTrace();
    } finally {
        if ( snapshot != null ) {
            snapshot.close();
        }
    }

    if ( BuildConfig.DEBUG ) {
        Log.d( "cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
    }

    return bitmap;

}

      

The problem is there is a lot of lag for large images and there is no good scrolling experience.

How can I improve this approach?

I can do public Bitmap putBitmap( String key )

to save the bitmap on a background thread, but there is a problem with public Bitmap getBitmap( String key )

that cannot be done on a background thread.

Thank.

+3


source to share





All Articles