android - 使用 getCacheDir() 缓存文件

标签 android caching dropbox

我想下载从 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 ,mDrawablenull

如果我评论 if 条件,它工作正常。但是每次都要下载图片。

编辑:

以上代码来自how to test for a file in cache

最佳答案

试试这个希望对你有帮助

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/

相关文章:

android - 删除 Android 的 Xamarin 表单中 Material 输入控制的下划线

java - Android:如何在设备重新启动后运行服务中的线程

dropbox - 无法使用 php api 将文件上传到 Dropbox?

javascript - 如何使用 javascript 为数据对创建动态数组?

c# - Xamarin ndk 从 c 函数获取字符串作为返回值

java - 对象与 Android Activity

angularjs - 缓存和延迟数据检索的可观察模式

javascript - BreezeJs - 带参数的 Ajax 调用,无缓存

javascript - 是否可以使用javascript清除浏览器缓存?

synchronization - dropbox是如何监控的?