Recently, some users have reflected that after saving the picture in the system library can not find the saved picture, then decided to thoroughly review and resolved.
There are two ways you can save a picture in adnroid:
- The first is to write your own method, the following code:
PublicStaticFileSaveImage(BitmapBmp){FileAppdir=NewFile(Environment.getExternalStorageDirectory(),"Boohee");If(!Appdir.Exists()){Appdir.Mkdir();}StringFileName=System.Currenttimemillis()+". jpg";FileFile=NewFile(Appdir,FileName);Try{FileOutputStreamFos=NewFileOutputStream(File);Bmp.Compress(Compressformat.Jpeg,100fos); fos. Flush (); fos. Close (); } catch (filenotfoundexception E) {e. Printstacktrace (); } catch (ioexception e ) {e. Printstacktrace (); }} /span>
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:
FinalMediascannerconnectionMSc=NewMediascannerconnection(Mcontext,NewMediascannerconnectionclient(){Publicvoidonmediascannerconnected(){msc. Scanfile (); } public void onscancompleted ( Span class= "NB" >string pathuri 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:
PublicStaticvoidSaveimagetogallery(ContextContext,BitmapBmp){//Save the picture firstFileAppdir=NewFile(Environment.getExternalStorageDirectory(),"Boohee");If(!Appdir.Exists()){Appdir.Mkdir();}StringFileName=System.Currenttimemillis()+". jpg";FileFile=NewFile(Appdir,FileName);Try{FileOutputStreamFos=NewFileOutputStream(File);Bmp.Compress(Compressformat.Jpeg,100,Fos);Fos.Flush();Fos.Close();}Catch(FileNotFoundExceptionE){E.Printstacktrace();}Catch(IOExceptionE){E.Printstacktrace();}//Then insert the file into the System galleryTry{Mediastore.Images.Media.Insertimage(Context.Getcontentresolver(),File.GetAbsolutePath(),FileName,Null); } catch (filenotfoundexception E) {e. Printstacktrace (); } // Last notification gallery update context. Sendbroadcast (new intent ( Intent. Action_media_scanner_scan_fileuri. Parse (+ path}
Android Save picture to System gallery