png*_*png 30 android image gallery
我正从我的应用程序中删除一个图像文件.我在做
new File(filename).delete ();
Run Code Online (Sandbox Code Playgroud)
这实际上是删除文件.但是图片仍然可以在画廊中看到.
在搜索时我发现我们应该使用
getContentResolver().delete(Uri.fromFile(file), null,null); 删除
但在这里我得到了例外:
未知的文件URL.java.lang.IllegalArgumentException:未知的URL文件:///mnt/sdcard/DCIM/Camera/IMG_20120523_122612.jpg
当我看到任何文件浏览器时,会出现此特定图像.请帮我解决这个问题.有没有其他方法可以在物理删除图像时更新图库
Tan*_*ien 35
我已经看到了很多建议使用的答案
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)
这有效但会导致Media Scanner重新扫描设备上的媒体.更有效的方法是通过Media Store内容提供商查询/删除:
// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
}
c.close();
Run Code Online (Sandbox Code Playgroud)
Dha*_*mar 32
使用下面的代码,它可能会帮助您.
File fdelete = new File(file_dj_path);
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + file_dj_path);
} else {
System.out.println("file not Deleted :" + file_dj_path);
}
}
Run Code Online (Sandbox Code Playgroud)
删除图像后刷新图库使用下面的代码发送广播
(对于<KITKAT API 14)
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)
对于> = KITKAT API 14,请使用以下代码
MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri)
{
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Run Code Online (Sandbox Code Playgroud)
因为:
ACTION_MEDIA_MOUNTED
Run Code Online (Sandbox Code Playgroud)
在KITKAT(API 14)中弃用.
2015年4月9日编辑
它的工作正常检查下面的代码
public void deleteImage() {
String file_dj_path = Environment.getExternalStorageDirectory() + "/ECP_Screenshots/abc.jpg";
File fdelete = new File(file_dj_path);
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.e("-->", "file Deleted :" + file_dj_path);
callBroadCast();
} else {
Log.e("-->", "file not Deleted :" + file_dj_path);
}
}
}
public void callBroadCast() {
if (Build.VERSION.SDK_INT >= 14) {
Log.e("-->", " >= 14");
MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
} else {
Log.e("-->", " < 14");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
Run Code Online (Sandbox Code Playgroud)
下面是日志
09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->? file Deleted :/storage/emulated/0/ECP_Screenshots/abc.jpg
09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->? >= 14
09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/? appName=com.example.sampleforwear, acAppName=/system/bin/surfaceflinger
09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/? 0
09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage? Scanned /storage/emulated/0:
09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage? -> uri=content://media/external/file/2416
Run Code Online (Sandbox Code Playgroud)
Cod*_*ody 23
File file = new File(photoUri);
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoUri))));
Run Code Online (Sandbox Code Playgroud)
这段代码对我有用,我认为它比重新安装整个SD卡更好 Intent.ACTION_MEDIA_MOUNTED
Dar*_*rai 14
要删除图像,
ContentResolver contentResolver = getContentResolver();
contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.ImageColumns.DATA + "=?" , new String[]{ imagePath });
Run Code Online (Sandbox Code Playgroud)
我尝试了所有这些解决方案,但在Android 6中没有运气.
最后,我发现这段剪辑的代码运行良好.
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});
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还在Android 4.4和5.1中对此进行了测试,结果非常完美.
在 Kotlin 中你可以这样做:
private fun deleteImage(path: String) {
val fDelete = File(path)
if (fDelete.exists()) {
if (fDelete.delete()) {
MediaScannerConnection.scanFile(this, arrayOf(Environment.getExternalStorageDirectory().toString()), null) { path, uri ->
Log.d("debug", "DONE")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47049 次 |
| 最近记录: |