Two ways to save a picture to a system album

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.