Android: add images to the system album,

Source: Internet
Author: User

Android: add images to the system album,

The following two methods may be used to save an image in Adnroid:

  • The first method is to write 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, 100, fos);        fos.flush();        fos.close();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}

The above code saves the Bitmap image to the specified path/sdcard/Boohee/. The file name is named after the current system time, but the image saved in this method is not added to the system image library.

  • The second method is to call the system-provided method for inserting the Image Library:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");

The method provided by the above system will save the bitmap object to the system image library, but this method cannot specify the saved path and name. The title and description parameters of the above method only insert fields in the database, the real image name is automatically allocated.

It seems that the second method above is the method we need to use, but it is a pity that the image of the second method inserted into the image library is not immediately displayed in the Image Library, we need to update the System Image Library immediately so that users can view the image immediately.

  • How to update the System Image Library
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

The above broadcast scans the whole SD card broadcast. If there are many things in your SD card that will be scanned for a long time, we cannot access the SD card during the scan, so this is a poor user experience, therefore, the following methods are available:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););

Alternatively, you can use the following methods:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() {         public void onMediaScannerConnected() {             msc.scanFile("/sdcard/Boohee/image.jpg", "image/jpeg");         }         public void onScanCompleted(String path, Uri uri) {             Log.v(TAG, "scan completed");             msc.disconnect();         }     });

The image path of the code above can be easily obtained by writing the image by yourself or inserting the image library by the system.

  • Ultimate perfect solution

Some may ask again here. If I want to save the image to the specified folder, what if I want the image to appear in the Image Library? The answer is yes. The sdk also provides the following method:

MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");

  

The second parameter of the preceding method is image path. In this way, you have to write a method to specify the image to the specified folder, call the above method to pass in the saved image path and notify the image library to update.

So I wrote a method. The complete code is as follows:

Public static void saveImageToGallery (Context context, Bitmap bmp) {// first save the image 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, 100, fos); fos. flush (); fos. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} // Insert the file to the system image library. try {MediaStore. images. media. insertImage (context. getContentResolver (), file. getAbsolutePath (), fileName, null);} catch (FileNotFoundException e) {e. printStackTrace ();} // finally notifies the image library to update the context. sendBroadcast (new Intent (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/




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.