How to delete a file from SD card? How to delete a file from SD card? android android

How to delete a file from SD card?


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

where selectedFilePath is the path of the file you want to delete - for example:

/sdcard/YourCustomDirectory/ExampleFile.mp3


Also you have to give permission if you are using >1.6 SDK

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

in AndroidManifest.xml file


Change for Android 4.4+

Apps are not allowed to write (delete, modify ...) to external storage except to their package-specific directories.

As Android documentation states:

"Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions."

However nasty workaround exists (see code below). Tested on Samsung Galaxy S4, but this fix does't work on all devices. Also I wouldn’t count on this workaround being available in future versions of Android.

There is a great article explaining (4.4+) external storage permissions change.

You can read more about workaround here.Workaround source code is from this site.

public class MediaFileFunctions {    @TargetApi(Build.VERSION_CODES.HONEYCOMB)    public static boolean deleteViaContentProvider(Context context, String fullname)     {       Uri uri=getFileUri(context,fullname);       if (uri==null)       {         return false;      }      try       {          ContentResolver resolver=context.getContentResolver();          // change type to image, otherwise nothing will be deleted          ContentValues contentValues = new ContentValues();          int media_type = 1;          contentValues.put("media_type", media_type);          resolver.update(uri, contentValues, null, null);          return resolver.delete(uri, null, null) > 0;       }       catch (Throwable e)       {          return false;       }    }   @TargetApi(Build.VERSION_CODES.HONEYCOMB)   private static Uri getFileUri(Context context, String fullname)    {      // Note: check outside this class whether the OS version is >= 11       Uri uri = null;       Cursor cursor = null;       ContentResolver contentResolver = null;      try      {          contentResolver=context.getContentResolver();          if (contentResolver == null)            return null;         uri=MediaStore.Files.getContentUri("external");          String[] projection = new String[2];          projection[0] = "_id";          projection[1] = "_data";          String selection = "_data = ? ";    // this avoids SQL injection          String[] selectionParams = new String[1];          selectionParams[0] = fullname;          String sortOrder = "_id";          cursor=contentResolver.query(uri, projection, selection, selectionParams, sortOrder);          if (cursor!=null)          {             try             {                if (cursor.getCount() > 0) // file present!                {                     cursor.moveToFirst();                   int dataColumn=cursor.getColumnIndex("_data");                   String s = cursor.getString(dataColumn);                   if (!s.equals(fullname))                      return null;                   int idColumn = cursor.getColumnIndex("_id");                   long id = cursor.getLong(idColumn);                   uri= MediaStore.Files.getContentUri("external",id);                }                else // file isn't in the media database!                {                     ContentValues contentValues=new ContentValues();                   contentValues.put("_data",fullname);                   uri = MediaStore.Files.getContentUri("external");                   uri = contentResolver.insert(uri,contentValues);                }             }             catch (Throwable e)             {                uri = null;             }            finally            {                cursor.close();            }         }       }       catch (Throwable e)       {          uri=null;       }       return uri;    } }