以编程方式从设备删除图像文件 [英] Delete image file from device programmatically
本文介绍了以编程方式从设备删除图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用我的应用程序删除图像文件?
How can I delete an image file using my app?
File file = new File("path"); //PATH is: /storage/sdcard0/DCIM/Camera/IMG_20160913_165933.jpg
-
file.delete();//试试这个,但不要删除文件....
-
boolean isDelete = file.delete();//这也不会删除...
-
context.deleteFile(file);//这个在我的示例中也不起作用....
推荐答案
如果您尝试删除图片或类似图片,则可能会遇到问题.可能会返回结果为true,但文件仍然存在.我浪费了很多时间,最适合我的是:
If you are trying to delete a picture or similar you may have issues. It may return to you that the result is true but the file still will exist. I have lost so much time and what best worked for me was:
private void deleteImage(File file) {
// 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();
}
这篇关于以编程方式从设备删除图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文