gpt4 book ai didi

android - 以编程方式从设备中删除图像文件

转载 作者:行者123 更新时间:2023-11-29 17:14:42 31 4
gpt4 key购买 nike

如何使用我的应用程序删除图像文件?

File file = new File("path"); //PATH is: /storage/sdcard0/DCIM/Camera/IMG_20160913_165933.jpg
  1. file.delete();//试试这个,但不要删除文件....
  2. boolean isDelete = file.delete();//这也不是删除...
  3. context.deleteFile(文件);//这个在我的示例中也不起作用....

最佳答案

如果您尝试删除图片或类似内容,您可能会遇到问题。它可能会返回结果为真,但文件仍然存在。我浪费了太多时间,最适合我的是:

  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();
}

关于android - 以编程方式从设备中删除图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530663/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com