问题描述
我删除从我的应用程序中的图像文件。我是做
I am deleting an image file from my application. I was doing
new File(filename).delete ();
这是实际删除该文件。但图像仍然是在画廊可见。
This was actually deleting the file. But the image was still visible in the gallery.
在搜索,我发现我们应该用
On search i found that we should use
getContentResolver()删除(Uri.fromFile(文件),NULL,NULL);
删除
但在这里我得到了异常:
But here i am getting the exception:
未知文件的URL。 java.lang.IllegalArgumentException异常:未知网址 文件:///mnt/sdcard/DCIM/Camera/IMG_20120523_122612.jpg
当我与任何文件浏览器中看到,这个特殊的形象是present。请帮我解决这个问题。是否有更新画廊任何其他方式在图像物理删除
When i see with any file browser, this particular image is present. Please help me to fix this issue. Is there any other way to update gallery when image is physically deleted
推荐答案
使用下面的code,它可以帮助你。
Use the code below, it may help you.
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);
}
}
删除低于code图像使用后刷新库:
to refresh gallery after deleting image use below code:
(对于其中;奇巧的API 14)
(for < KITKAT API 14)
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
有关> =奇巧API使用14以下code
For >= KITKAT API 14 use below code
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);
}
});
由于:
ACTION_MEDIA_MOUNTED
是pcated在奇巧(API 14)。
is deprecated in KITKAT(API 14).
EDITED 2015年4月9日
低于code及其工作精细检查
its working fine check below code
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())));
}
}
下面
是日志
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
这篇关于机器人:删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!