How to save image to external storage gallery in android

I am trying to write an image file to a shared gallery folder in a specific directory, but I continue to receive an error message that I cannot open the file because its directory.

So far, I have been next

//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 name of the application. Thus, all photos saved by the application will go into this folder / directory, but I continue to receive an error

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

Even if I'm not trying to put it in a directory and pass the variable path as a file, for example

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

I do not get an error, however the photo is still not displayed in the gallery.

*** Answer The problem was that when I ran this code initially, it created a DIRECTORY named testing.png because I was unable to create the directory before creating the IN file in the directory. So the solution is to create a directory first and then write a separate file to it, for example

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

Please note that you may need to log into your repository and manually delete the directory if you made the same mistake as me, starting with

+4
source share
5 answers

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

 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory; File outputDir= new File(path); outputDir.mkdirs(); File newFile = new File(path+"/"+"test.png"); FileOutputStream out = new FileOutputStream(newFile); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); 
+8
source

Your code is correct, only small changes need the following,

 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, be sure to grant WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml file.

+2
source

If you get this error while working with Android Emulator; you need to enable SD card storage on the emulator.

+1
source

Use this method:

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

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

0
source
 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(); } 
0
source

Source: https://habr.com/ru/post/1440690/


All Articles