7

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.

What I have so far is the following

//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);

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

/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)

Even if I don't try to put it in a directory and cast the variable path as a File like

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

I don't get the error however the photo is still not showing up in the gallery.

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

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);

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

Brian
  • 4,328
  • 13
  • 58
  • 103
  • sometimes gallery need to be refreshed. Go to your folder path through ddms and check if your new file is created or not. – sachy Oct 19 '12 at 03:44
  • Yeah I did that multiple times withno luck – Brian Oct 19 '12 at 03:46
  • For scanning your new file see this [post][1] [1]: http://stackoverflow.com/questions/4646913/android-how-to-use-mediascannerconnection-scanfile/5815005#5815005 – sachy Oct 19 '12 at 03:47
  • see this http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage/7887114#7887114 – NagarjunaReddy Oct 19 '12 at 03:55

6 Answers6

13

You are trying to write into a directory instead of file. try this

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;   
File outputDir= new File(path);   
outputDir.mkdirs();   
File newFile = new File(path + File.separator + "test.png");
FileOutputStream out = new FileOutputStream(newFile);   
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);   
David Artmann
  • 4,272
  • 1
  • 16
  • 24
sachy
  • 739
  • 7
  • 13
  • What had happened is at one point my code wrote the full path as a directory. So i had a directory called "testing.png" than I tried to write the bitmap into that directory. This method works – Brian Oct 19 '12 at 23:20
3

Your code is correct, only little changes needs as follows,

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;

    // First Create Directory
    File outputFile = new File(path);
    outputFile.mkdirs();

    // Now Create File
    outputFile = new File(path,"testing.png");
    FileOutputStream out = new FileOutputStream(outputFile);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

Also don't forget to give WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml file.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
2

If you are getting this error while working on Android Emulator; you need to enable SD Card storage on the emulator.

Abhishek Mehta
  • 1,447
  • 1
  • 14
  • 16
2
public static String SaveImage(Bitmap finalBitmap) {

        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/FolderName");
        if(!myDir.exists())
            myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".png";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
Afzaal Iftikhar
  • 233
  • 2
  • 5
2

getExternalStoragePublicDirectory is now deprecated and you should use

context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
lxknvlk
  • 2,744
  • 1
  • 27
  • 32
0

Use this way :

bitmap.compress(CompressFormat.JPEG, 100,  new FileOutputStream("/mnt/sdcard/" + new Date().getTime() + ".jpg"));`

Path : Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + file name

SilentKiller
  • 6,944
  • 6
  • 40
  • 75