I'm currently trying to save a file in internal memory for my android application but I can't manage to do it correctly!
Here's my method to store a bitmap:
private void storeBitmapInMemory(String id) throws IOException {
URL newurl = new URL(PictureUrl);
Bitmap bm = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
// The openfileOutput() method creates a file on the phone/internal storage in the context of your application
final FileOutputStream fos = context.openFileOutput(PREFIX + id +".jpeg", Context.MODE_PRIVATE);
// Use the compress method on the BitMap object to write image to the OutputStream
bm.compress(CompressFormat.JPEG, 100, fos);
}
Everything looks fine when I execute this method, but in really nothing save... When I try to read the file from memory with this method:
private Bitmap getStoredBitmapFromMemory(int idAvatar) {
File cacheDir = context.getCacheDir();
File f = new File(cacheDir, PREFIX + Integer.toString(idAvatar) + ".jpeg");
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream(fis);
}
It's throwing this exception:
05-02 10:50:22.487: W/System.err(25805): java.io.FileNotFoundException: /data/data/com.example.xxxx/cache/AVATAR_400021371.jpeg: open failed: ENOENT (No such file or directory)
05-02 10:50:22.497: W/System.err(25805): at libcore.io.IoBridge.open(IoBridge.java:416)
05-02 10:50:22.497: W/System.err(25805): at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-02 10:50:22.497: W/System.err(25805): at com.example.xxxx.Performer.getStoredBitmapFromMemory(Performer.java:140)
What's wrong with my storeBitmapInMemory??