There are two ways you can save a picture in adnroid:
- The first is to write your own method, the following code:
public static file SaveImage (Bitmap bmp) { file Appdir = new file (Environment.getexternalstoragedirectory (), "Boohee "); if (!appdir.exists ()) { appdir.mkdir (); } String fileName = System.currenttimemillis () + ". jpg"; File File = new file (Appdir, fileName); try { FileOutputStream fos = new FileOutputStream (file); Bmp.compress (Compressformat.jpeg, N, FOS); Fos.flush (); Fos.close (); } catch (FileNotFoundException e) { e.printstacktrace (); } catch (IOException e) { e.printstacktrace (); c16/>}}
The above code is to save the bitmap image to the specified path/sdcard/boohee/, the file name is named after the current system time, but this method saves the picture is not added to the System diagram library
- The second is to invoke the system-supplied method of inserting the library:
MediaStore.Images.Media.insertImage (Getcontentresolver (), Bitmap, "title", "description");
Calling the system's own method will save the bitmap object to the system library, but this method can not specify the path and name of the saved, the title of the above method, the description parameter is only inserted in the database field, the real picture name System is automatically assigned.
It seems that the second method above is the method we want to use, but unfortunately the second way to insert the image of the library is not immediately displayed in the gallery, and we need to update the system library immediately so that users can see this image immediately.
- Ways to update system libraries
Sendbroadcast (new Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ())));
The above radio is to scan the entire SD card broadcast, if you SD card inside a lot of things will be scanned for a long time, in the scan we are not able to access the SD card, so this kind of user reflects very bad, so below we also have the following methods:
Sendbroadcast (new Intent (Intent.action_media_scanner_scan_file, Uri.fromfile ("New FILE" ("/sdcard/ Boohee/image.jpg ")););
Or there are the following methods:
Sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file, Uri.fromfile (NewFile ("/sdcard/boohee/image.jpg"))););FinalMediascannerconnection MSC =NewMediascannerconnection (Mcontext,Newmediascannerconnectionclient () { Public voidonmediascannerconnected () {Msc.scanfile ("/sdcard/boohee/image.jpg", "Image/jpeg"); } Public voidonscancompleted (String path, Uri uri) {log.v (TAG,"Scan Completed"); Msc.disconnect (); } });
The image path of the above code can be easily obtained either by writing your own method or by inserting a system into the library.
- The Ultimate Perfect solution
Then there may be someone to ask, if I want to save the picture to the specified folder, but also need to appear in the gallery? The answer is yes, and the SDK also provides a way to:
MediaStore.Images.Media.insertImage (Getcontentresolver (), "Image Path", "title", "description");
The second parameter of the above method is the image path, so that there is the idea of the first way to write the picture to the specified folder, and then call the above method to the newly saved picture path into, and finally notify the library update.
So write a method, the complete code is as follows:
Public Static voidSaveimagetogallery (context context, Bitmap bmp) {//Save the picture firstFile Appdir =NewFile (Environment.getexternalstoragedirectory (), "Boohee"); if(!appdir.exists ()) {Appdir.mkdir (); } String fileName= System.currenttimemillis () + ". jpg"; File File=NewFile (Appdir, fileName); Try{FileOutputStream fos=Newfileoutputstream (file); Bmp.compress (Compressformat.jpeg,100, FOS); Fos.flush (); Fos.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } //then insert the file into the system Gallery Try{MediaStore.Images.Media.insertImage (Context.getcontentresolver (), File.getabsolutepath (), fil ENAME,NULL); } Catch(FileNotFoundException e) {e.printstacktrace (); } //finally notify the gallery updateContext.sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file, Uri.parse ("file://" +Path))) ;}
Original Blog Address: http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/
android--add images to the system album