0

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??

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Shinnyx
  • 2,144
  • 2
  • 14
  • 21
  • Have you give the permission? – Abx May 02 '13 at 15:00
  • Check this http://stackoverflow.com/questions/11620641/android-error-open-failed-enoent – Pragnani May 02 '13 at 15:02
  • Have you checked the PREFIX directory exists? Do this in the storeBitmapInMemory() method before creating FileOutputStream File dir = new File(PREFIX); Try navigating to where you think the file is in a file manager and check it is actually there before you spend too long trying to debug the file open method if(!(dir.exists() && dir.isDirectory())) dir.mkdirs(); – rcbevans May 02 '13 at 15:04
  • 1
    you should use openFileInput to read the file you stored with openFileOutput – Blackbelt May 02 '13 at 15:19
  • Sorry, PREFIX equals "AVATAR_". it's for my file name ex: AVATAR_100234.jpeg (the number is an ID). For the directory, I've tried to navigate to my "data" folder for my application... but the package name of my app isn't in the data folder... I don't know why... And in my exception error the path is : /data/data (why two data? There's only one in fileexplorer...). The com.example.xxxx doesn't exist, too! :( – Shinnyx May 02 '13 at 15:20
  • Wow, thanks blackbelt! Everything is working fine now that I'm using openFileINput :) Thank you! I wish I could accept this as the anwser ! – Shinnyx May 02 '13 at 15:35
  • @blackbelt create an answer with "you should use openFileInput to read the file you stored with openFileOutput" – David Wasser May 02 '13 at 16:32

1 Answers1

1

you have to use openFileInput to read a file you stored with openFileOutput.

here the doc for openFileInput

Blackbelt
  • 156,034
  • 29
  • 297
  • 305