Hir*_*ela 4 android image file
如何使用我的应用程序删除图像文件?
File file = new File("path"); //PATH is: /storage/sdcard0/DCIM/Camera/IMG_20160913_165933.jpg
Run Code Online (Sandbox Code Playgroud)
file.delete(); // try this one but not delete file....boolean isDelete = file.delete(); //this also not delete...context.deleteFile(file); // thisn one also not working in my example....如果您尝试删除图片或类似图片,则可能会遇到问题。它可能会返回结果为 true,但文件仍然存在。我浪费了很多时间,对我来说最有效的是:
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11183 次 |
| 最近记录: |