小编f.o*_*old的帖子

文件缓存和文件权限

我在我的应用程序中实现缓存并在4.2.x设备上部署它时遇到问题.

当我在app cache目录中保存文件时,我无法读取它.我使用具有root权限的文件管理器来浏览缓存文件夹,并且文件正确地存在于我的缓存文件夹中,但是当我尝试读取它们时,获取文件未找到错误.

此问题仅与4.2 android版本有关.在4.1-一切正常

这是我用来编写文件的代码

public static void writeFile(Bitmap bmp, File f) {

        if (bmp == null)
            return;

        FileOutputStream out = null;

        try {
            out = new FileOutputStream(f);
            boolean res = bmp.compress(Bitmap.CompressFormat.PNG, 80, out);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (Exception ex) {
            }
        }
        f.setReadable(true, false);
    }
Run Code Online (Sandbox Code Playgroud)

这就是我构造文件对象的方式

File bitmapFile = new File(ImageManager.getInstance().getCacheDir(), key);
Run Code Online (Sandbox Code Playgroud)

这是如何获取缓存路径

if (Config.USE_INTERNAL_CACHE == false && sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
        cacheDir = …
Run Code Online (Sandbox Code Playgroud)

android image-caching android-file android-permissions

5
推荐指数
0
解决办法
4228
查看次数