Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: DiskLruCache not saved after application closed
    text
    copied!<p>I'm new to SO and also new to Android programming! So pardon me if the question is trivial.</p> <p>Anyway, I'm trying to create a gallery application that will show the thumbnails of all media files (videos and pictures) in a folder. My application is running good except for the long waiting time to process the image to get its sampled bitmap. </p> <p>I've settled this using memory cache (LruCache) but it only solves the problem when user stays in the app and navigate around. Everytime the user closes the app and re-opens it, it'll re-generate the cache again.</p> <p>After doing some readings online, I decided to add disk cache on top of memory cache, hoping that it'll solve the problem. But even after I implemented it, the problem still persists. Not sure if I'm doing it wrong. </p> <p>Below is my code to initialize the memory cache and disk cache.</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String path = Environment.getExternalStorageDirectory().getAbsolutePath(); if (savedInstanceState != null) { displayManager.cancelAsyncTask(); path = savedInstanceState.getString(ARG_POSITION); }else{ // Get memory class of this device, exceeding this amount will throw an // OutOfMemory exception. final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass(); // Use 1/8th of the available memory for this memory cache. final int cacheSize = 1024 * 1024 * memClass / 8; mMemoryCache = new LruCache&lt;String, Bitmap&gt;(cacheSize) { protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in bytes rather than number of items. return bitmap.getByteCount(); } }; Log.d("DEBUG_CACHE","context is "+this); File cacheDir = getDiskCacheDir(this, DISK_CACHE_SUBDIR); Log.d("DEBUG_CACHE", "cache dir is "+cacheDir.getAbsolutePath()); new InitDiskCacheTask().execute(cacheDir); } ArrayList&lt;File&gt; folderList = dataManager.getFolderListWithPhotoOrVideo(path); ArrayList&lt;File&gt; thumbList = dataManager.getAllPhotosAndVideos(path); displayManager.setCurrentPath(path); displayManager.setParent(this); displayManager.setMemCache(mMemoryCache); displayManager.setFolderList(folderList); displayManager.setThumbnails(thumbList); } </code></pre> <p>Below is how I get the thumbnails from cache if exists and how I add the bitmap to the cache.</p> <pre><code>@Override protected Void doInBackground(Void... params) { int noOfThumbnails = thumbnails.size(); for(int j=0;j&lt;noOfThumbnails;j++){ String filePath = thumbnails.get(j).getName(); if(isPhoto(filePath)){ //if file is an image file Uri uri = Uri.fromFile(new File(thumbnails.get(j).getAbsolutePath())); Bitmap thumb = getBitmapFromMemCache(filePath); if(thumb==null){ thumb = getBitmapFromDiskCache(filePath); if(thumb==null){ thumb = getPreview(uri); addBitmapToCache(filePath,thumb); } } bitmaps.add(thumb); }else{ Bitmap thumb = getBitmapFromMemCache(filePath); if(thumb==null){ thumb = getBitmapFromDiskCache(filePath); if(thumb==null){ thumb = ThumbnailUtils.createVideoThumbnail(thumbnails.get(j).getAbsolutePath(),MediaStore.Images.Thumbnails.MICRO_KIND); addBitmapToCache(filePath,thumb); } } bitmaps.add(thumb); } double progress = ((double)(j+1)/(double)noOfThumbnails)*100; publishProgress(new Double(progress).intValue()); if(isCancelled()){ Log.d("DEBUG", "doInBackGround. Task cancelled"); return null; } } return null; } public void addBitmapToCache(String key, Bitmap bitmap) { if (getBitmapFromMemCache(key) == null) { mMemoryCache.put(key, bitmap); } // Also add to disk cache synchronized (parent.mDiskCacheLock) { try { if (parent.mDiskLruCache != null &amp;&amp; parent.mDiskLruCache.get(key) == null) { parent.mDiskLruCache.put(key, bitmap); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public Bitmap getBitmapFromMemCache(String key) { return (Bitmap) mMemoryCache.get(key); } public Bitmap getBitmapFromDiskCache( String key ) { Bitmap bitmap = null; Snapshot snapshot = null; try { snapshot = parent.mDiskLruCache.get( key ); if ( snapshot == null ) { return null; } final InputStream in = snapshot.getInputStream( 0 ); if ( in != null ) { final BufferedInputStream buffIn = new BufferedInputStream( in, Utils.IO_BUFFER_SIZE ); bitmap = BitmapFactory.decodeStream( buffIn ); } } catch ( IOException e ) { e.printStackTrace(); } finally { if ( snapshot != null ) { snapshot.close(); } } return bitmap; } </code></pre> <p>For the DiskLruCache.java, I used the google source file (can be found <a href="https://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/util/DiskLruCache.java?r=fcd13b09ef50f290dc0681ebec74fbdfcac969a5" rel="nofollow">here</a>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload