Android saves images to the system and provides solutions to related problems

Source: Internet
Author: User

In the Application Gallery, users are usually provided with the function of saving images, so that users can save their favorite images to the system album.

This function is actually very easy to implement. The system provides the ready-made API:

Simply put, this line of code:

 

[Java] MediaStore. Images. Media. insertImage (getContentResolver (), mBitmap ,"","");

MediaStore. Images. Media. insertImage (getContentResolver (), mBitmap ,"","");
The returned value of this method and the path of the inserted Image
[Java] String url = MediaStore. Images. Media. insertImage (getContentResolver (), mBitmap ,"","");

String url = MediaStore. Images. Media. insertImage (getContentResolver (), mBitmap ,"","");

However, after being called on many machines (such as G7 and G11), you cannot see the saved image in the album. You need to restart the machine and then display it in the system album, greatly affecting User Experience

This is because of the cache problem of the machine system. We need to refresh the photo album environment after inserting the image:


There are two methods:


[Java]
SendBroadcast (new Intent (Intent. ACTION_MEDIA_MOUNTED, Uri. parse ("file: //" + Environment. getExternalStorageDirectory ())));

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


[Java]
Final MediaScannerConnection msc = new MediaScannerConnection (mContext, new MediaScannerConnectionClient (){
Public void onMediaScannerConnected (){
Msc. scanFile ("/sdcard/image.jpg", "image/jpeg ");
}
Public void onScanCompleted (String path, Uri uri ){
 
Msc. disconnect ();
}
});

Final MediaScannerConnection msc = new MediaScannerConnection (mContext, new MediaScannerConnectionClient (){
Public void onMediaScannerConnected (){
Msc. scanFile ("/sdcard/image.jpg", "image/jpeg ");
}
Public void onScanCompleted (String path, Uri uri ){

Msc. disconnect ();
}
}); It should be noted that sending a broadcast or directly calling a scan to scan the entire SD card will actually be less efficient and increase power consumption.


As mentioned above, the return value corresponding to the method for inserting an image is the absolute path saved in the image. Therefore, we only need to obtain the path and send a broadcast to scan the path.


[Java]
SendBroadcast (new Intent (Intent. ACTION_MEDIA_MOUNTED, Uri. parse ("file: //" + Environment. getExternalStorageDirectory () + picPath )));

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

 

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.