从图库或照片中删除图片 Android 8.0



我想根据图片的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

我错过了什么吗?


更新

我在运行代码之前添加了运行时权限。运行代码后,它总是变成">文件未删除"由其他块组成。

您还需要检查您是否具有权限,如果没有,则在运行时上获取它。请求清单文件中的权限是不够的。您可以使用以下两种方法来查看您是否具有编写权限,如果没有,则获取它:

protected boolean checkPermission() {
int result1 = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (result1 == PackageManager.PERMISSION_GRANTED  ) {
return true;
} else {
return false;
}
}
protected void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}

}

像这样改变

String path = selectedImageUri.getPath() 
deleteMethod(path);

然后删除里面的方法

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

好吧,既然你说你也获得了在运行时写入的权限,也许你应该在确定文件存在之后和尝试删除它之前使用setWritable()方法。

在 Android 6.0 之后,会在运行时授予一些权限。您可以使用此代码。


另外,请检查 https://developer.android.com/about/versions/marshmallow/android-6.0-changes

public  boolean hasStoragePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission error");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}

以下是我目前用于以编程方式删除下载的apk的代码。

final File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + giftapk);
if (file.exists()) {
if (file.delete()) {
System.out.println("file Deleted :" + file);
} else {
System.out.println("file not Deleted :" + file);
}
}

最新更新