Android refresh media library after deleting pictures

Sometimes the picture is deleted in the App, and when you call the picture selector, you will see that the deleted picture still exists (it cannot be displayed). This is because the picture is deleted, but the record in the media library still exists, and there is no synchronous refresh The media library will be refreshed after the phone restarts.

We can call the following code to refresh the media library after deleting the file:

Situation 1:

deleteSingle picture, When you know the exact picture path:

    /**
     * 刷新媒体数据,让文件立刻显示在相册中
     *
     * @param mContext
     * @param filePath 文件(图片)全路径; ./aa/bb/cc.jpg
     */
    public static void refreshMedia(Context mContext, String filePath) {
        MediaScannerConnection.scanFile(mContext,
                new String[]{filePath},
                new String[]{"image/jpeg", "image/png", "image/jpg"},
                (path, uri) -> {
                    LegoLog.d("onScanCompleted:" + path);
                });
    }

Situation 2:

What is deletedfolderWhen (multiple pictures), when it is not clear the specific path of each picture:

    /**
     * 删除文件后更新数据库  通知媒体库更新文件夹
     *
     * @param context
     * @param filepath 文件夹路径)要求尽量精确,以防删错
     */
    public static void updateFileFromDatabase(Context context, String filepath) {
        String where = MediaStore.Audio.Media.DATA + " like \"" + filepath + "%" + "\"";
        // _data like "/storage/emulated/0/ipw200/image/chn0_0/%"
//        LegoLog.d("删除条件:" + where);
        int i = context.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, where, null);
        if (i > 0) {
            LegoLog.d("媒体库更新成功!");
        }
    }
Published 45 original articles · Like 24 · Visits 50,000+

Guess you like

Origin blog.csdn.net/zhijiandedaima/article/details/105074698