0

I want to delete a file from the removable sd card, I tried many ways but nothing did the job.

Tried:

file.delete();

and

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

and

DocumentFile documentFile = DocumentFile.fromFile(file);
documentFile.delete();

and

DocumentsContract.deleteDocument(context.getContentResolver(),
  Uri.fromFile(file );

none of which deletes the file

Jack
  • 5,354
  • 2
  • 29
  • 54
aboahmad
  • 11
  • 1
  • 6
  • yes i added these permissions @jackz314 – aboahmad Apr 05 '19 at 21:21
  • files.delete() , don't do any thing – aboahmad Apr 05 '19 at 21:26
  • java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.ContentProviderClient.call(java.lang.String, java.lang.String, android.os.Bundle)' on a null object reference this error appers when i use this DocumentsContract.deleteDocument(context.getContentResolver(), Uri.fromFile(file ); @jackz314 – aboahmad Apr 05 '19 at 21:28
  • i am trying to work on file explore and i could use the file for many things, for example one of the paths /storage/extSdCard/Download @jackz314 – aboahmad Apr 05 '19 at 21:34
  • it returns true @jackz314 – aboahmad Apr 05 '19 at 21:47
  • You know what, I totally forgot about the restrictions Android imposes, look at CommonsWare's answer, I think he explains it quite clearly. – Jack Apr 05 '19 at 21:49
  • ok, thanks for your time – aboahmad Apr 05 '19 at 21:54

1 Answers1

1

I want to delete a file from the removable sd card

In general, you can't.

If you put the file in one of the Context locations (getExternalFilesDirs(), etc.), then you should be able to delete it, using delete() on a File object.

If this is some other file, you do not have permission to do anything with it, including delete it.

And on Android Q, you will not have much access to external or removable storage at all.

You are welcome to use the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT, ACTION_OPEN_DOCUMENT_TREE) and work with content that way.

i am trying to work on file explore

Android Q is severely restricting that entire app category. I recommend that you build something else.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • the proplem that i am facing on android lolipop and up. is there any solution for these devices? – aboahmad Apr 05 '19 at 21:52
  • @aboahmad: Use the Storage Access Framework. See [this blog post](https://commonsware.com/blog/2017/11/15/storage-situation-removable-storage.html) for more about how removable storage works with Android. – CommonsWare Apr 05 '19 at 21:53
  • for lolipop and up, you would need to put it in the "Context locations" like described in the answer (or just set the "context location" where the file is, e.g., the entire external storage). Check this [answer](https://stackoverflow.com/a/26765884/8170714) – Jack Apr 05 '19 at 21:54
  • i'll check these context locations , and hope it works, thanks a lot @jackz314 – aboahmad Apr 05 '19 at 21:58