我的问题是我从外部存储器中删除了图像,并且黑色占位符仍然指的是删除的图像位置。
这是代码,请帮我把它拿出来。
提前致谢。
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});
}
}
答案 0 :(得分:0)
我一直将此功能用于我的音乐应用,但也适用于画廊。
首先找到所需条目的_id
,然后构建正确的uri以提交给删除查询。
public void deleteFromMediaStore(String pathToDelete, Context context) {
ContentResolver contentResolver = context.getContentResolver();
if (contentResolver != null) {
Cursor matchingIndex = contentResolver.query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data"}, "_data=?", new String[]{pathToDelete}, null);
if(matchingIndex != null && matchingIndex.getCount() > 0) {
matchingIndex.moveToFirst();
while (!matchingIndex.isAfterLast()) {
context.getContentResolver().delete(ContentUris.withAppendedId(Media.EXTERNAL_CONTENT_URI, (long) matchingIndex.getInt(matchingIndex.getColumnIndex("_id"))), null, null);
matchingIndex.moveToNext();
}
matchingIndex.close();
}
}
}
答案 1 :(得分:0)
你可以发送广播
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
或者如果你想回调
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
null,
new OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
}
});