Summary of Android library update synchronization problems (including adding pictures and deleting pictures)

Summary of Android library update synchronization problems (including adding pictures and deleting pictures)

Android sometimes needs to update the library to synchronize information such as pictures.
For example: 1. Download the picture successfully and save it to the local place. At this time, go to the gallery to check and find that it is still not updated;
2. Empty the pictures in the application, delete the pictures in a directory recursively, and the gallery is not refreshed in time.

Add pictures and update key codes of the gallery

Call in the place where the library needs to be updated (for example, in the callback of downloading pictures successfully)

// update gallery
new SingleMediaScanner(McpttApp.getGlobalContext(), Define.SAVE_PATH + fileName, new ScanListener() {   
        public void onScanFinish() {
            Log.d(Define.TAG, "--onScanFinish");

The implementation of is as follows:

package com.mcptt.common;

import android.content.Context;  
import android.util.Log;  

public class SingleMediaScanner implements MediaScannerConnectionClient {  

public interface ScanListener{  
    public void onScanFinish();  

private MediaScannerConnection mMs;  
//private File mFile;  
private String mFilePath;
private ScanListener listener;  
private final String TAG = "SingleMediaScanner";

public SingleMediaScanner(Context context, File f,ScanListener l) {  
    listener = l;  
    mFilePath = f.getAbsolutePath();  
    mMs = new MediaScannerConnection(context, this);  

public SingleMediaScanner(Context context, String filePath,ScanListener l) {  
    listener = l;  
    mFilePath = filePath;  
    mMs = new MediaScannerConnection(context, this);  

public void onMediaScannerConnected() {  
    Log.d(TAG, "--onMediaScannerConnected--file:"+mFilePath);
    mMs.scanFile(mFilePath, null);  

public void onScanCompleted(String path, Uri uri) {  
    Log.d(TAG, "--onScanCompleted--path:"+path+"--uri:"+uri);


Delete pictures and update key codes of Gallery

Application scenario: in the application settings, there is a cache clearing function, which is mainly used to clear the pictures and video files under the application directory (such as: / storage/sdcard0/eChat).

The code to delete the picture is as follows:

private boolean deleteImage(String imgPath) {
    ContentResolver resolver = this.getContentResolver();
    Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=?",
        new String[] { imgPath }, null);
    boolean result = false;
    if (null != cursor && cursor.moveToFirst()) {
        long id = cursor.getLong(0);
        Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Uri uri = ContentUris.withAppendedId(contentUri, id);
        Log.d(TAG, "--deleteImage--uri:"+uri);
        int count = this.getContentResolver().delete(uri, null, null);
        result = count == 1;
    } else {
        File file = new File(imgPath);
        result = file.delete();
        Log.d(TAG, "--deleteImage--imgPath:"+imgPath+"--result:"+result);
        return result;

Tags: Android Java

Posted on Mon, 30 Mar 2020 10:40:07 -0700 by villas