我想下载从 Dropbox 下载的图像并缓存它们以供进一步使用:
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);
如果代码没有进入if block ,mDrawable
为null
。
如果我评论 if 条件,它工作正常。但是每次都要下载图片。
编辑:
最佳答案
试试这个希望对你有帮助
String path = context.getFilesDir().getAbsolutePath() + File.separator + entry.fileName();
File file = new File(path);
if (file.exists()) {
// File exists
} else {
// File does not exist
}
关于android - 使用 getCacheDir() 缓存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23538092/