Seshu Vinay
Seshu Vinay

Reputation: 13588

How to delete an image from sd card

How can I delete an image file from sd card. I have tried:

File file=new File(filepath);  
file.delete();

but I could not delete it. when I try to inspect it in debug mode, file.delete() is returning false. I have also added permission in my Manifest file.

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

I also tried with:

public abstract boolean deleteFile (String name)

method from andoid context. Even that returns false.

Upvotes: 4

Views: 5601

Answers (2)

Paresh Mayani
Paresh Mayani

Reputation: 128428

Instead of passing static value for sd-card storage directory, it would be better to use getExternalStorageDirectory() to retrieve exact value of sd-card directory. (Because it may be /sdcard or /mnt/sdcard so):

String dirPath = Environment.getExternalStorageDirectory().getPath();

Detailed example is here: Check and Gets the Android external storage directory

Upvotes: 2

thi
thi

Reputation: 48

File file = new File(selectedFilePath);
boolean deleted = file.delete();

Upvotes: 3

Related Questions