How to save a bitmap in a phone gallery?

I take a picture and show it in the image. Then I get the bitmap from the image in my activity, and when I click the button, I want to save this bitmap in the phone gallery. What should I do?

+4
source share
3 answers

calling this function in Button onClick

private void saveImage() { File myDir=new File("/sdcard/saved_images"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image-"+ n +".jpg"; 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 (Exception e) { e.printStackTrace(); } } 

check save bitmap

+7
source

try it

  Bitmap toDisk = Bitmap.createBitmap(w1,h1,Bitmap.Config.ARGB_8888); setBitmap(toDisk); Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background); Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false); try { toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/image".jpg"))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+1
source

First you need to first open the path to the External Storage folder, and then the directory where the gallery's images are stored. On most Android models, this is / mnt / sdcard / pictures, but I do not suggest hardcoding this way instead of using

 Environment.getExternalStorageDirectory(); 

After that, just create the file path in this directory and use outputStream to write your bitmap to this directory.

0
source

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


All Articles