我想根据其 URI 从设备中删除图库或照片应用程序中的图片。我在互联网上尝试了几种方法,但没有找到。我调用了下面的方法deleteMethod(getPath(selectedImageUri));这两个方法定义在这里。private void deleteMethod(String file_dj_path) { File fdelete = new File(file_dj_path); if (fdelete.exists()) { if (fdelete.delete()) { System.out.println("file Deleted :" + file_dj_path); Toast.makeText(getApplicationContext(), "file Deleted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "file not Deleted", Toast.LENGTH_SHORT).show(); System.out.println("file not Deleted :" + file_dj_path); } }}public String getPath(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(uri, projection, null, null, null); if (cursor != null) { //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else return null;}我在清单中添加权限。像, <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />我的提供者看起来像: <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path path="Android/data/com.***.calculator/" name="files_root" /> <external-path path="." name="external_storage_root" /></paths>我有这样的uri::/storage/emulated/0/DCIM/Camera/IMG_20180804_181447.jpg我错过了什么吗?
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
改成这样
String path = selectedImageUri.getPath()
deleteMethod(path);
然后里面的delete方法
File file = new File(new URI(path));
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + uri.getPath());
} else {
System.out.println("file not Deleted :" + uri.getPath());
}
}
添加回答
举报
0/150
提交
取消