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:
MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");
The returned value of this method and the path of the inserted Image
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:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Or:
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 the scan to scan the entire SD card will actually be less efficient and will 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.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()+ picPath)));