I have a problem with caching in my application and deployment on 4.2.x devices.
When I save the file in the application cache directory, I cannot read it. I use the file manager with root privileges to examine the cache folder, and the files are correctly present in my cache folder, but when I try to read them, get a file that is not found. .
This issue is only related to version 4.2 of android. In 4.1- everything 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 create an object file
File bitmapFile = new File(ImageManager.getInstance().getCacheDir(), key);
Here's how to 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 is correctly written in
/data/data/my.app.package/cache/
This is the code I use to read 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 causes 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 already added this permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
, , , , ? ** , , , , android 4.2, . ** .setReadable(true, false), .