I have a problem implementing caching in my app and deploying it on 4.2.x devices.
When i save a file in the app cache directory i'm unable to read it. I use a file manager with root permissions to explore cache folder and the files are correctly present in my cache folder but when i try to read them a get a file not found error.
This problem is related only to 4.2 android version. In 4.1- all works fine
This is the code i use to write the file
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);
}
This is how i construct the file object
File bitmapFile = new File(ImageManager.getInstance().getCacheDir(), key);
This is how get the cache path
if (Config.USE_INTERNAL_CACHE == false && sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = MyApp.getInstance().getExternalCacheDir();
} else {
cacheDir = MyApp.getInstance().getCacheDir();
}
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
At this point my file are correctly written to
/data/data/my.app.package/cache/
This is the code i use to read the files
public BitmapDrawable readFile(String fileName) {
FileInputStream in = null;
try {
in = new FileInputStream(getCacheDir()+"/"+fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(in);
if (bmp == null) {
return null;
}
return new BitmapDrawable(MyApp.getInstance().getResources(), bmp);
}
This always generate this error:
01-22 11:23:47.158: W/System.err(20660): java.io.FileNotFoundException: /data/data/my.app.package/cache/File Name: open failed: ENOENT (No such file or directory)
01-22 11:23:47.158: W/System.err(20660): at libcore.io.IoBridge.open(IoBridge.java:416)
01-22 11:23:47.158: W/System.err(20660): at java.io.FileInputStream.<init(FileInputStream.java:78)
01-22 11:23:47.158: W/System.err(20660): at java.io.FileInputStream.<init(FileInputStream.java:105)
01-22 11:23:47.158: W/System.err(20660): atmy.app.package.instanceManager.ImageManager.readFile(ImageManager.java:246)
01-22 11:23:47.158: W/System.err(20660): atmy.app.package.instanceManager.ImageManager$ImageQueueManager.run(ImageManager.java:186)
01-22 11:23:47.158: W/System.err(20660): at java.lang.Thread.run(Thread.java:856)
01-22 11:23:47.158: W/System.err(20660): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-22 11:23:47.158: W/System.err(20660): at libcore.io.Posix.open(Native Method)
01-22 11:23:47.158: W/System.err(20660): atlibcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-22 11:23:47.158: W/System.err(20660): at libcore.io.IoBridge.open(IoBridge.java:400)
01-22 11:23:47.158: W/System.err(20660): ... 5 more
related to this line:
in = new FileInputStream(getCacheDir()+"/"+fileName);
I have already added this permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The path and the name file related to this error are correct and the file exists in the cache folder, so why i cannot read them? **I think it could be an user permission related issue due to the new multiuser features introduced in android 4.2 but i cant figure it out. **I also try to set the file .setReadable(true, false)
but nothing. Thanks