Android 从SD卡中删除图像后如何刷新多媒体资料
删除Android SD卡上的图像时,有时图像会被正确删除,但在gallery中仍然保留已删除图像的预览。当点击它时,它将作为黑色图像加载。要解决此问题,我需要运行Android 从SD卡中删除图像后如何刷新多媒体资料,android,android-mediascanner,Android,Android Mediascanner,删除Android SD卡上的图像时,有时图像会被正确删除,但在gallery中仍然保留已删除图像的预览。当点击它时,它将作为黑色图像加载。要解决此问题,我需要运行MediaScanner。但是这段代码不起作用,而且,预览的回顾图像仍然保留在图库中 任何人都知道如何解决这个问题 Uri contentUri = Uri.fromFile(file); Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
MediaScanner
。但是这段代码不起作用,而且,预览的回顾图像仍然保留在图库中
任何人都知道如何解决这个问题
Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
sendBroadcast(mediaScanIntent);
试试这个:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
将此添加到您的清单中:
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
尽管
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
仅限于4.4中的系统应用程序
下面是另一个解决方案。传递已删除或添加的图像的路径,如果图像已删除,则传递图像传递true
,如果已将图像添加到gallery,则传递false
/**
* Scanning the file in the Gallery database
*
* @param path
* @param isDelete
*/
private void scanFile(String path, final boolean isDelete) {
try {
MediaScannerConnection.scanFile(context, new String[] { path },
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
if (isDelete) {
if (uri != null) {
context.getContentResolver().delete(uri,
null, null);
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
您应该将其从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});
}
}
}
对于删除时的每个文件,请使用以下命令 基特卡特 这对我有用
try {
context.getContentResolver().delete(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media.DATA + "='"
+ new File(fileUri).getPath() + "'", null);
} catch (Exception e) {
e.printStackTrace();
}
我一直在寻找C#Xamarin代码,并为Xamarin Android找到了此代码
您可以将多个已删除文件的地址/路径传递给ScanFile(上下文,字符串[]),以便Mediascanner获得通知并更新库
此代码在android 4.4中不起作用。它崩溃并显示以下logcat权限拒绝:不允许发送广播android.intent.action.MEDIA_MOUNTEDNope它在android 4.4I中仍然崩溃我尝试了您的代码,但仍然在库中显示已删除图像的预览。任何关于它为什么不起作用的线索。我正在安卓4.3&above@AndroidDev这是现在唯一的办法。。对我来说很好。如果图像被删除,请传递true。好的,我会让你知道。这就是我如何使用你的代码,在这里它总是为Uri返回空值。你能检查一下我的代码中到底出了什么问题吗。我正在测试Note2(Android版本4.3)@AndroidDev您是如何修复的?应用程序的ContentResolver(作为context.getContentResolver()检索)您的代码适用于Android 4.3,但在安卓4.4中,它仍然在Gallery中显示删除的图像预览。当我在4.1和5.1上测试它时,这似乎很有魅力,因为我只有这两款设备。。关于4.4不确定。此代码在棉花糖设备中不起作用。有什么想法吗?@Nithinjith对我来说,M在工作。在堆栈上提出一个新问题。
Android.Media.MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] { deletedImageFilePath}, null, null);