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

时间:2018-08-17 12:23:33

标签: java android photos android-8.1-oreo

我想根据其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

我错过了什么吗?


更新

我在运行代码之前添加了运行时权限。我运行代码后,它始终会变为“ 文件未删除”,包含else块。

4 个答案:

答案 0 :(得分:0)

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

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


}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这样更改

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

答案 3 :(得分:0)

好吧,既然您说您也获得了在运行时进行写操作的权限,那么也许在建立文件存在之后并尝试将其删除之前,应该使用setWritable()方法。