0

I have a File object, file. This file points to a file on the SD card. How can I delete this file on Android api version 5.+ ? I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission in Manifest also called in in run time. I have tried file.delete() it works for internal and external storage but file is not deleted on SD Card/Secondary storage. Any suggestions?

Tousif Osman
  • 287
  • 2
  • 12
  • If you have the `File` object and the read/write permission for external storage, you can just call `file.delete()`. That being said, try reading documentation on the class you are working with before posting it as a question – ᴛʜᴇᴘᴀᴛᴇʟ Mar 07 '18 at 03:57
  • @osman have a look on this answer. this may help you [click here](https://stackoverflow.com/questions/1248292/how-to-delete-a-file-from-sd-card) – Mohd Qasim Mar 07 '18 at 03:57
  • `file.delete()` not working for files on SD card. According to what I found google restricted SD card write access after api version 4.4 – Tousif Osman Mar 07 '18 at 04:00

2 Answers2

0

You can delete any file or dir by this method:

File f = new File("your path");
f.delete();

and don't forget to add permission in menifest

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

and above version 6.0 you need to give runtime permission like this:

final String[] NECESSARY_PERMISSIONS = new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE};

    if ((ContextCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
    {
            //Permission granted.
    }
    else
    {       
            // Request for permission
            ActivityCompat.requestPermissions(getApplicationContext(),
                    NECESSARY_PERMISSIONS, 123);
    }
SahdevRajput74
  • 754
  • 7
  • 18
  • I have permission. `file.delete()` not working on SD card. I have already tried this. – Tousif Osman Mar 07 '18 at 04:01
  • you are run on 6.0 > version ? – SahdevRajput74 Mar 07 '18 at 04:02
  • I am developing on android Lollipop. I have considered run time permission. I can see the app has the WRITE permission in the app Settings. **Please note, I can delete the same file on internal and external storage. However, file is not deleted on SD CARD/Secondary Storage** – Tousif Osman Mar 07 '18 at 04:06
  • https://stackoverflow.com/questions/47111244/cant-access-the-secondary-storage-the-external-removable-micro-sd-card – SahdevRajput74 Mar 07 '18 at 04:16
0

@osman may be your not implementing run time permissions concept

Mohd Qasim
  • 896
  • 9
  • 20
  • Yes I have considered run time `WRITE_EXTERNAL_STORAGE` permission. **Please note, I can delete the same file on internal and external storage. However, file is not deleted on SD CARD/Secondary Storage** – Tousif Osman Mar 07 '18 at 04:08
  • @osman have a look on this link https://android.stackexchange.com/questions/96500/how-to-allow-apps-to-delete-sd-card-content – Mohd Qasim Mar 07 '18 at 04:16
  • I have already saw this question. The only feasible option seems to be the ' systems storage provider'. But couldn't find any working example. – Tousif Osman Mar 07 '18 at 04:23