This code works perfectly until you clear cache from Device's Settings/App/your app menu.
/**
* @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;
}
I've tried to delete this file and create it again, as a workaround, to release lock by some other(system) app, and that failed also. Only device's reboot do solve that problem. Any help would be appreciated. Thank you!