gpt4 book ai didi

android - 从SDCard删除图像后如何刷新图库

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:24 28 4
gpt4 key购买 nike

删除Android SD卡上的图片时,有时图片被正确删除,但图库中仍然保留已删除图片的预览。点击它时,它会加载为黑色图像。要解决它,我需要运行 MediaScanner。但是此代码不起作用,评论图像的预览仍然保留在图库中。

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

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

最佳答案

你应该从 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});
}
}
}

关于android - 从SDCard删除图像后如何刷新图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22606080/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com