Detailed explanation of Android pictures stored in system album update display example
In the process of developing Android, we cannot avoid the possibility of making a custom album or creating a folder locally to store the pictures we need. Take the album as an example. For example, we create a test folder, store it in this specified test folder after taking a photo, and then display it in the album, just like the effect of WeChat. It will be displayed immediately after the shooting. However, in the actual development process, we cannot update and display the picture immediately after saving a picture. We need to restart our phone to display it in the system album.
Here is a method to insert the system gallery:
(getContentResolver(), mBitmap, "", "");
This method allows you to insert the picture into the system gallery. However, it will not be displayed immediately after insertion, and a broadcast needs to be sent:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, ("file://"+ ())));
To notify the system to scan the entire SD card. If there are more things in the SD card, the longer the scan will be, and we cannot access the SD card during the scan, so this implementation method is not friendly to users.
Here we can use the method given to us by the system api:MediaScannerConnection to scan and update:
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); (1, 2000); mConnection =new MediaScannerConnection(mContext,this); } @Override public void onMediaScannerConnected() { ("/sdcard/", "image/jpeg"); } @Override public void onScanCompleted(String s, Uri uri) { ("huan", "scan completed"); (); }
However, during the use of this method, I found a problem with this method: the incoming context will be held by MediaScannerConnection, resulting in oom. Therefore, you need to pay attention to the activity during use.
In addition, we can also broadcast by specifying the path, so that we can avoid scanning the entire SD card:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, ("file://" + () + picPath)));
I will share the tips and precautions for image update with you, I hope it will be helpful to you.