0

I am trying to delete a file on an external SD storage in android by using the following code:

filename = "/storage/extSdCard/AAA/testtitle.mp3"
File file = new File(filename)    
file.delete()

I am also using the two permissions

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

in the Manifest (outside the application declaration), and I am using the code suggested here. The device does not seem to be a Marshmellow device (first answer), and the suggested solution in the third answer also does not help. I still get the same error:

remove failed: EACCES (Permission denied) : /storage/extSdCard/AAA/testtitle.mp3

What else can I try?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • You do not have arbitrary read/write access to [removable storage](https://commonsware.com/blog/2017/11/15/storage-situation-removable-storage.html) on Android 4.4+. I do not know how you got that `File`, but your problems begin there. Use the Storage Access Framework to get a `Uri` pointing to the content on removable storage, then use `delete()` on `DocumentFile` to try to delete the content. – CommonsWare Feb 04 '18 at 14:30
  • I tried: `DocumentFile cfile = DocumentFile.fromFile(file); cfile.delete();` but got the same error again... – Alex Feb 04 '18 at 14:41
  • Use the Storage Access Framework to get a `Uri` pointing to the content on removable storage. Use `DocumentFile.fromSingleUri()` to get a `DocumentFile` pointing to that piece of content represented by the `Uri` from the Storage Access Framework. Then, use `delete()` on the `DocumentFile`. – CommonsWare Feb 04 '18 at 15:05
  • I get a new error: `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` – Alex Feb 04 '18 at 15:13
  • Code used: `Uri myuri = Uri.parse(filename); DocumentFile cfile = DocumentFile.fromSingleUri(getApplicationContext(), myuri);` – Alex Feb 04 '18 at 15:14
  • `Use the Storage Access Framework to get a Uri pointing to the content on removable storage. Use DocumentFile.fromSingleUri() to get a DocumentFile pointing to that piece of content represented by the Uri from the Storage Access Framework. Then, use delete() on the DocumentFile. ` – greenapps Feb 04 '18 at 15:14
  • 1
    `Use the Storage Access Framework to get a Uri pointing to the content on removable storage.`. It seems as if you fail to start in this way. – greenapps Feb 04 '18 at 15:15
  • I am using: `Uri myuri = Uri.parse(filename);`. Is that incorrect? – Alex Feb 04 '18 at 15:28
  • You did not get that filename from the Storage Access Framework, in part because content does not have a filename. As I wrote originally, I do not know how you got that `File`, but your problems begin there. – CommonsWare Feb 04 '18 at 15:30
  • I am not quite following. Why can't you provide some example code, starting from the filename of the file which is "/storage/extSdCard/AAA/testtitle.mp3"? I am not an expert in android! – Alex Feb 04 '18 at 15:32
  • "Why can't you provide some example code, starting from the filename of the file which is "/storage/extSdCard/AAA/testtitle.mp3"?" -- because that filename is useless. Please read [the blog post that I linked to in my first comment](https://commonsware.com/blog/2017/11/15/storage-situation-removable-storage.html). – CommonsWare Feb 04 '18 at 15:44
  • I want to remove a file with the given filename. I tried to do the following: `File file = new File(filename); boolean deleted = file.delete();`. What is the problem with this code? – Alex Feb 04 '18 at 15:45
  • I do not understand the blog. There is no code in that blog. Just description of some things I do not quite understand, and comparison of version etc... – Alex Feb 04 '18 at 15:47
  • Question updated with actual code... – Alex Feb 04 '18 at 15:55
  • I have no idea what you are talking about the "Storage Access Framework" or why the `file.delete()` does not work. So I have found a solution that seems to be way easier: 1. Instead of deleting a file, I add the filename to a text file. 2. After a while I connect the phone to Linux and open this text file with a python script. 3. The python script then removes the files. Because in python you can just delete a file damn easy.... Thanks anyway... – Alex Feb 04 '18 at 16:23

0 Answers0