【问题标题】:delete a picture from gallery or photos android 8.0从图库或照片中删除图片 android 8.0
【发布时间】:2018-08-17 12:23:33
【问题描述】:

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

我错过了什么吗?


更新

我在运行代码之前添加了运行时权限。在我运行代码后,它总是“file not Deleted”包含 else 块。

【问题讨论】:

  • 执行代码时会发生什么?它会崩溃吗?它什么都不做吗?
  • 'if (fdelete.exists()) {'文件一开始就不存在。
  • i got uri like this: :/storage/emulated/0/DCIM/Camera/IMG_20180804_181447.jpg 那不是 uri。不清楚你是如何得到的。 /storage/emulated/0/DCIM/Camera/IMG_20180804_181447.jpg 将是一个文件系统路径。

标签: java android photos android-8.1-oreo


【解决方案1】:

您还需要检查您是否有权限,如果没有在运行时获得它。在清单文件中请求权限是不够的。您可以通过这两种方法来查看您是否有写入权限,如果没有获得权限:

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


}

【讨论】:

    【解决方案2】:

    这样改

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

    然后里面的deleteMethod

    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】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 在运行时添加权限。如果可能,您可以运行此代码。
      • 当您将 deleteMethod() 与特定 uri 一起使用时,它是否有效?不使用 getPath() 方法。
      • 我编辑了我的答案,我看不出有什么不同,但你可以检查一下并与我的代码进行比较
      猜你喜欢
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      相关资源
      最近更新 更多