title: Writing Bitmap to external storage (sdcard)
body: <p>I'm trying to save Bitmap that I have in certain activity to External Storage in a directory that I create for this purpose. The method for saving the image to sdcard (external memory, not external sd) is in a different class (and different package) so I assumed I will need the context, but I can't find any place to give the context (tried <code>openFileOutput</code> but it gets <code>String fileName</code> that can't contain path seperators). When I run my code I get this error in log cat:</p>
  
  <pre><code>04-11 22:13:14.899: E/error(13833): /mnt/sdcard/myTomatoes/covers/378194.PNG: open                  failed: ENOENT (No such file or directory)
  04-11 22:13:14.899: W/System.err(13833): java.io.FileNotFoundException: /mnt/sdcard/myTomatoes/covers/378194.PNG: open failed: ENOENT (No such file or directory)
  04-11 22:13:14.899: W/System.err(13833):    at libcore.io.IoBridge.open(IoBridge.java:416)
  04-11 22:13:14.899: W/System.err(13833):    at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:88)
  04-11 22:13:14.899: W/System.err(13833):    at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:73)
  04-11 22:13:14.899: W/System.err(13833):    at     BL.ImageMethods.SaveImageToMemory(ImageMethods.java:60)
  04-11 22:13:14.899: W/System.err(13833):    at com.example.mytomatoes.MovieDetailsActivity$4.onClick(MovieDetailsActivity.java:337)
  04-11 22:13:14.899: W/System.err(13833):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
  04-11 22:13:14.899: W/System.err(13833):    at android.os.Handler.dispatchMessage(Handler.java:99)
  04-11 22:13:14.899: W/System.err(13833):    at android.os.Looper.loop(Looper.java:137)
  04-11 22:13:14.899: W/System.err(13833):    at android.app.ActivityThread.main(ActivityThread.java:5039)
  04-11 22:13:14.899: W/System.err(13833):    at java.lang.reflect.Method.invokeNative(Native Method)
  04-11 22:13:14.899: W/System.err(13833):    at java.lang.reflect.Method.invoke(Method.java:511)
  04-11 22:13:14.899: W/System.err(13833):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  04-11 22:13:14.899: W/System.err(13833):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  04-11 22:13:14.899: W/System.err(13833):    at dalvik.system.NativeStart.main(Native Method)
  04-11 22:13:14.899: W/System.err(13833): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
  04-11 22:13:14.899: W/System.err(13833):    at libcore.io.Posix.open(Native Method)
  04-11 22:13:14.899: W/System.err(13833):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
  04-11 22:13:14.899: W/System.err(13833):    at libcore.io.IoBridge.open(IoBridge.java:400)
  </code></pre>
  
  <p>Those are my permissions:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>This is my method for saving a file:</p>
  
  <pre><code>public static void SaveImageToMemory(Context context, Bitmap img, int rottenId) {
          Log.i("SAVE IMAGE", "start save");
          File sd = Environment.getExternalStorageDirectory();
          File location = new File(sd.getAbsolutePath()+ "/myTomatoes/covers");
          location.mkdir();
          File dest = new File(location, rottenId + ".PNG");
  
          try {
              Log.i("SAVE IMAGE", "trying to save: " + dest.getPath());
              FileOutputStream fos = new FileOutputStream(dest);
  
              img.compress(Bitmap.CompressFormat.PNG, 100, fos);
              fos.flush();
              fos.close();
          } catch (IOException e) {
              Log.e("error", e.getMessage());
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>All the messed up lines for creating the <code>File</code> object are result of my try outs... The original code was:</p>
  
  <pre><code>    File location = new File(Environment.getExternalStorageDirectory() + "/myTomatoes/covers");
      File dest = new File(location, rottenId + ".PNG");
  </code></pre>
  
  <p>I also tried to first create <code>ByteArrayOutputStream</code> and compress the Bitmap to it and then do <code>fos.write(mByteArrayOpStream.toByteArray())</code> and it didn't work.</p>
  
  <p>By the way i'm using emulator but I also checked this on few "Galaxy S"s and the same error happens... (when I try to browse for the file in the phone or in DDMS I can see that it doesn't even create the folder...</p>