0

此代码完美运行,直到您从设备的设置/应用程序/您的应用程序菜单中清除缓存。

/**
 * @param context te get packageName & {@code cacheDir} from internal storage

 * @return file for {@code cacheDir} either SD card or internal storage. Or {@code null} if cacheDir doesn't exist
 */
public static File getCacheDir(Context context) throws IOException {
    File cacheDir;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String cacheDirPath = getApplicationCacheDirPath(context.getPackageName());
        cacheDir = new File(Environment.getExternalStorageDirectory() + File.separator + cacheDirPath);
    } else {
        cacheDir = context.getCacheDir();
    }

    if (cacheDir != null && !cacheDir.exists()) {
        if (!cacheDir.mkdirs()) {
            throw new IOException("Can't use cacheDir");
        }
    }
    return cacheDir;
}

作为一种解决方法,我尝试删除此文件并再次创建它,以释放其他(系统)应用程序的锁定,但这也失败了。只有设备的重启才能解决这个问题。任何帮助,将不胜感激。谢谢!

4

0 回答 0