title: How do i save an image in external storage gallery in android
body: <p>I'm trying to write an image file into the public gallery folder in a specific directory but I keep getting an error that I can't open the file because its a directory.</p>
  
  <p>What I have so far is the following</p>
  
  <pre><code>//set the file path
      String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
  
      File outputFile = new File(path,"testing.png");
  
  
      outputFile.mkdirs();
  
      FileOutputStream out = new FileOutputStream(outputFile);
      bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
  </code></pre>
  
  <p>Where directory is the application name. So all the photos saved by the application will go into that folder/directory, but I keep getting the error</p>
  
  <pre><code>/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)
  </code></pre>
  
  <p>Even if I don't try to put it in a directory and cast the variable path as a File like</p>
  
  <pre><code>File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  </code></pre>
  
  <p>I don't get the error however the photo is still not showing up in the gallery. </p>
  
  <p>***Answer
  The problem was that when I ran this code originally it created a DIRECTORY named testing.png because I failed to create the directory before creating the file IN the directory. So the solution is to make the directory first then write into it with a separate file like so</p>
  
  <pre><code>String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + directory;
  
  //directory is a static string variable defined in the class
  
              //make a file with the directory
      File outputDir = new File(path);
  
              //create dir if not there
      if (!outputDir.exists()) {
           outputDir.mkdir();
  
      }
  
              //make another file with the full path AND the image this time, resized is a static string
      File outputFile = new File(path+File.separator+resized);
  
      FileOutputStream out = new FileOutputStream(outputFile);
      bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
  </code></pre>
  
  <p>Note you may need to go into your storage and manually delete the directory if you made the same mistake i did to begin with</p>