The first: Use the API of the system directly:
Contentresolver CR = Getcontentresolver (); String URL = MediaStore.Images.Media.insertImage (CR, Bmp,string.valueof (System.currenttimemillis ()), "");
However, this way you have to refresh the library:
Sendbroadcast (New Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ())));
However, such a method is only suitable for Android 4.4 under the mobile phone, if more than 4.4 of the phone will be error. It is therefore recommended to write in a different way.
Another: direct use of file streams to save to albums
File Tempfile = new file (Environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM). GetPath () + "/" + String.valueof (System.currenttimemillis ()) + ". png"), if (Tempfile.exists ()) {Tempfile.delete ();} try {tempfile.createnewfile ();} catch (IOException e) {e.printstacktrace ();} FileOutputStream FOut = null;try {fOut = new FileOutputStream (tempfile);} catch (FileNotFoundException e) {e.printstacktr Ace ();} Bmp.compress (Bitmap.CompressFormat.PNG, FOut), try {fout.flush (); Fout.close ();} catch (IOException e) {//TODO: Handle Exceptione.printstacktrace ();}
Finally, the whole method is posted:
/** * Save the picture in ImageView to the system register */private void Saveimagetosysalbum () {if (Fileutil.issdcardexist ()) {bitmapdrawable Bmpdrawable = (bitmapdrawable) mfullimageview.getdrawable (); Bitmap bmp = Bmpdrawable.getbitmap (), if (BMP! = null) {try {/*contentresolver CR = Getcontentresolver (); String URL = MediaStore.Images.Media.insertImage (CR, Bmp,string.valueof (System.currenttimemillis ()), ""); */file Tempfile = new File (Environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM). GetPath () + "/" + String.valueof (System.currenttimemillis ()) + ". png"), if (Tempfile.exists ()) {Tempfile.delete ();} try {tempfile.createnewfile ();} catch (IOException e) {e.printstacktrace ();} FileOutputStream FOut = null;try {fOut = new FileOutputStream (tempfile);} catch (FileNotFoundException e) {e.printstacktr Ace ();} Bmp.compress (Bitmap.CompressFormat.PNG, FOut), try {fout.flush (); Fout.close ();} catch (IOException e) {//TODO: Handle Exceptione.printstacktrace ();} Toast.maketext (This, getString (R.STRING.SAVE_SUCC), Toast.length_short). Show ();} catch (Exception e) {e.printstacktrace ();}} else {Toast.maketext (this, getString (R.string.no_iamge_save_fail), Toast.length_short). Show ();}} else {Toast.maketext (this, getString (R.string.no_sdcard_save_fail), Toast.length_short). Show (); String release = Android.os.Build.VERSION.RELEASE; String tempid = release.substring (0, 3), if (Double.parsedouble (tempid) >= 4.4) {//Android 4.4 or above version number use this. The following uses the mediascannerconnection.scanfile inside the Else statement (This,new string[]{environment.getexternalstoragepublicdirectory ( ENVIRONMENT.DIRECTORY_DCIM). GetPath () + "/"}, Null,null);} else {sendbroadcast (new Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ()))); Mediascannerconnection.scanfile (This,new string[]{environment.getexternalstoragepublicdirectory ( ENVIRONMENT.DIRECTORY_DCIM). GetPath () + "/"}, Null,null); }}
Two ways to save a picture to a system album