Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: remove an image from sd card

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?

like image 742
Matteo Avatar asked Jul 15 '11 12:07

Matteo


People also ask

Why can't I delete pictures from my SD card?

Photos cannot be deleted from SD card | Annoying SD card is write-protected. The SD card photos or files which you want to delete are open. SD card file system is corrupt or damaged.

How do I remove items from my SD card?

Right-click the files you want to delete and click "Delete." Alternatively, you can drag them from the hard disk window to the Recycle Bin (Windows) or Trash bin (Mac OS X). Close your SD card's hard disk window when you finish.


1 Answers

Why would you make it that complex?

You can do it as simple as this:

getContentResolver().delete(Utils.imgUri, null, null);
like image 185
Kalpesh Avatar answered Oct 12 '22 22:10

Kalpesh