There are two ways to save images to the system album: album

Source: Internet
Author: User

There are two ways to save images to the system album: album

First, use system APIs directly:

ContentResolver cr = getContentResolver();String url = MediaStore.Images.Media.insertImage(cr, bmp,String.valueOf(System.currentTimeMillis()), "");

However, you must refresh the image library in this way:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

However, this method is only applicable to phones with Android 4.4 or lower, and an error is reported for phones with more than 4.4. Therefore, we recommend that you use the second method;

Type 2: directly save the file stream to the album

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.printStackTrace();}bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);try {fOut.flush();fOut.close();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}


Finally, paste the entire method:

/*** Save the photo slices in the ImageView to the system album */private void SaveImageToSysAlbum () {if (FileUtil. isSdCardExist () {BitmapDrawable BMP drawable = (BitmapDrawable) mFullImageView. getDrawable (); Bitmap bmp = bmp drawable. 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. printStackTrace ();} bmp. compress (Bitmap. compressFormat.. PNG, 100, fOut); try {fOut. flush (); fOut. close ();} catch (IOException e) {// TODO: handle finished tione. 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) {// This is used for Android 4.4 and later versions. The following uses MediaScannerConnection in the else statement. scanFile (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 );}}





How can I store images in my own album?

Look at this android call system album and System camera function instance www.eoeandroid.com/thread-172978-1-1.html call system album and System camera function instance www.eoeandroid.com/thread-69679-1-1.html view the original post>

Remember to adopt it.

How can I directly save images on the webpage to Baidu album?

Write a new log in the Baidu space, and insert the log Content into your image. Baidu will automatically save the image to the 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.