Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i save an image in external storage gallery in android

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

like image 890
Brian Avatar asked Oct 19 '12 03:10

Brian


People also ask

Where are the files stored in external storage in Android?

The files stored on the external storage are located in sdcard/Android/data/app_name/.

What is external storage in Android?

In general there are two types of External Storage: Primary External Storage: In built shared storage which is “accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer”. Example: When we say Nexus 5 32 GB. Secondary External Storage: Removable storage. Example: SD Card.


4 Answers

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);   
like image 195
sachy Avatar answered Oct 20 '22 06:10

sachy


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.

like image 40
Lucifer Avatar answered Oct 20 '22 07:10

Lucifer


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

like image 2
Abhishek Mehta Avatar answered Oct 20 '22 07:10

Abhishek Mehta


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();
    }
like image 2
Afzaal Iftikhar Avatar answered Oct 20 '22 07:10

Afzaal Iftikhar