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 )));