11

I need to remove an image from sd card chosen by user. In my Activity, after an user select an image from gallery, i execute this code:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Utils.imgUri = data.getData();
            Utils.imgPath = getPath(Utils.imgUri);
            File file = new File(Utils.imgPath);
            boolean deleted = file.delete();
        }
    }
}

where getPath method is:

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if(cursor!=null){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    else return null;
}

The images are correctly removed but in the gallery still remain a preview of the removed image. When i tap on it, is loaded a black image..

so, How can I update the gallery previews, after I delete some images from my app code?

Matteo
  • 276
  • 1
  • 4
  • 18

7 Answers7

36

Why would you make it that complex?

You can do it as simple as this:

getContentResolver().delete(Utils.imgUri, null, null);
Goran Jovic
  • 9,040
  • 3
  • 40
  • 74
Kalpesh
  • 1,619
  • 18
  • 27
4

Resolved adding:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.

same problem here

Community
  • 1
  • 1
Matteo
  • 276
  • 1
  • 4
  • 18
  • Doesn't work. Leaves image in the gallery. Instructions unclear above, I added your line immediately after the file.delete(); function is called. If this is correct, code does not work as intended. – Bisclavret Sep 02 '15 at 05:03
0

simple one line ;)

 new File(uri.getPath()).delete();

and in manifest must use these permissions

android.permission.WRITE_EXTERNAL_STORAGE

Muhammad Adil
  • 3,250
  • 1
  • 23
  • 30
0

Have you set the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission in the manifest file?

az4dan
  • 631
  • 1
  • 10
  • 28
0

Are you sure that the file path is correct? Because the way you do the actual delete should be fine, see this SO thread: How to delete a file from SD card?

Should the /mnt/ really be there? Also, do you have the permissions to delete files from the storage? (android.permission.WRITE_EXTERNAL_STORAGE)

Community
  • 1
  • 1
Stefan H Singer
  • 5,304
  • 2
  • 23
  • 26
  • Yes, I have this permission. What do you mean with "/mnt/ really be there?". I get the path of the image from the Uri, with the getPath() method. – Matteo Jul 15 '11 at 13:34
  • It seems that the path is wrong though, since file.exists() also returns false. Here another (but similar) way to get the path is used: http://stackoverflow.com/questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android – Stefan H Singer Jul 15 '11 at 13:40
0

in the parameter "data" you have the Uri too, just do "data.getUri()". Also, are you testing in a real devices? if so and if it is a samsung, it isn't work (see this thread).

Community
  • 1
  • 1
Finuka
  • 730
  • 7
  • 15
  • I get the Uri with "data.getUri()", then i get the path with "getPath()" method. Is there a way to delete a file using its Uri? – Matteo Jul 15 '11 at 14:13
  • if using a File object with that uri doesnt work, then try using a contentResolver.delete(uri,null,null) – Finuka Jul 15 '11 at 15:06
-1

add below code in onDestroy method:

 if(myFile.exists())
 myFile.delete();

and don't forget to add permission in Manifest file

android.permission.WRITE_EXTERNAL_STORAGE

Einzig7
  • 545
  • 6
  • 20
Rohit Jain
  • 231
  • 1
  • 4
  • 7