Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching files using getCacheDir()

I wanted to download images that are downloaded from Dropbox and cache them for further use:

String cachePath = mContext.getCacheDir().getAbsolutePath() + entry.fileName();
File cacheFile = new File(cachePath);
//cacheFile.exists() returns true after 1st call
if(!cacheFile.exists()){
  //If cache doesn't exist, download the file
  mFos = new FileOutputStream(cachePath);
  mApi.getThumbnail(path, mFos, ThumbSize.BESTFIT_320x240,
                              ThumbFormat.JPEG, null);
}
mDrawable = Drawable.createFromPath(cachePath);
mImageView.setImageDrawable(mDrawable);

The mDrawable is null if the code doesn't enter the if block.

If I comment the if condition it works fine. But downloads the images every time.

Edit:

The above code is from how to test for a file in cache

like image 509
Housefly Avatar asked May 08 '14 09:05

Housefly


1 Answers

Try this hope helps you

String path = context.getFilesDir().getAbsolutePath() + File.separator + entry.fileName();
        File file = new File(path);

        if (file.exists()) {
            // File exists
        } else {
            // File does not exist
        }
like image 67
Biraj Zalavadia Avatar answered Nov 14 '22 19:11

Biraj Zalavadia