delete a picture from gallery or photos android 8.0

331 views Asked by At

I want to delete a picture from gallery or photo apps from device according to its URI. I tried several approach around internet but no way found.

I called below method

deleteMethod(getPath(selectedImageUri));

These two method defination is here.

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

I add permission in manifest. Like,

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

My provider looks like:

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

i got uri like this:

:/storage/emulated/0/DCIM/Camera/IMG_20180804_181447.jpg

Am i missing anything?


Update:

I added runtime permission before run the code. After i run the code it always goes "file not Deleted" consists else block.

4

There are 4 answers

0
Sotiris S. Magionas On

You also need to check if you have the permission and if not acquire it on RUNTIME. Asking for the permissions in your manifest file is not enough. You could use these two methods to see if you have permission to write and if not acquire it:

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


}
3
keser On

After Android 6.0, some permissions are granted at runtime. You can use this code.


also, check 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;
    }
}

Following is a code I currently use to delete a downloaded apk programmatically.

    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);
        }
    }
1
sasikumar On

Change like this

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

then deleteMethod inside

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
Sotiris S. Magionas On

Well, since you say you also take the permission to write on runtime, perhaps you should use the setWritable() method after you have established the file exists and before trying to delete it.