如何从SD卡中删除图像后刷新图库 [英] How to refresh Gallery after deleting image from SDCard

查看:215
本文介绍了如何从SD卡中删除图像后刷新图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除对Android手机的SD卡上的图像时,有时图像是正确删除,但在画廊仍然被删除的图像的preVIEW。当在其上攻丝,它被加载为黑色图象。要解决它,你需要运行MediaScanner。但是,这code将无法工作,仍然综述图片的preVIEW留在库中。

任何人知道如何解决这个问题。

 乌里contentUri = Uri.fromFile(文件);
意图mediaScanIntent =新的意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
sendBroadcast(mediaScanIntent);
 

解决方案

您应该从mediaStore删除

 公共静态无效deleteFileFromMediaStore(最终ContentResolver的ContentResolver的,最后的文件的文件){
    字符串canonicalPath;
    尝试 {
        canonicalPath = file.getCanonicalPath();
    }赶上(IOException异常E){
        canonicalPath = file.getAbsolutePath();
    }
    最后开放的URI = MediaStore.Files.getContentUri(外部);
    最终诠释结果= contentResolver.delete(URI,
            MediaStore.Files.FileColumns.DATA +=?,新的String [] {canonicalPath});
    如果(结果== 0){
        最后弦乐absolutePath = file.getAbsolutePath();
        如果(!absolutePath.equals(canonicalPath)){
            contentResolver.delete(URI,
                    MediaStore.Files.FileColumns.DATA +=?,新的String [] {absolutePath});
        }
    }
}
 

When deleting the images on Android’s SD Card, sometime the images are correctly removed but in the gallery still remain a preview of the removed image. When tapping on it, it is loaded as a black image. To resolve it, you need to run MediaScanner. But this code won't work and still the preview of review image remain in the Gallery.

Anyone knows how to resolve this.

Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri); 
sendBroadcast(mediaScanIntent);

解决方案

You should delete it from mediaStore

public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
    String canonicalPath;
    try {
        canonicalPath = file.getCanonicalPath();
    } catch (IOException e) {
        canonicalPath = file.getAbsolutePath();
    }
    final Uri uri = MediaStore.Files.getContentUri("external");
    final int result = contentResolver.delete(uri,
            MediaStore.Files.FileColumns.DATA + "=?", new String[] {canonicalPath});
    if (result == 0) {
        final String absolutePath = file.getAbsolutePath();
        if (!absolutePath.equals(canonicalPath)) {
            contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
        }
    }
}

这篇关于如何从SD卡中删除图像后刷新图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆