2

My app is able to open and process certain files. So you can tap on such a file for example in a file manager, and my app will be offered to open it. The intent filter in the manifest file is set up something like this.

<intent-filter>
  <action android:name="android.intent.action.VIEW" />

  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />

  <data android:mimeType="text/plain" />
</intent-filter>

Reading and processing the file like this works fine:

Context.getContentResolver().openInputStream(getIntent().getData());

The issue is, that I want to delete the file after processing it. (If the user checks a checkbox for it) Users typically don't need the file anymore after opening it with my app.

Currently I am only able to do it if the file is provided to me as a file uri (starting with file://). In this case this code works fine:

new File(contentUri.getPath()).delete();

But I don't know how to delete the file if it is provided as a content uri. (starting with content://)

For example this returns null, and does nothing:

Context.getContentResolver().delete(getIntent().getData(), null, null);
Twindek
  • 91
  • 2
  • 8
  • Where are you getting the `Uri` from? – CommonsWare Nov 21 '16 at 20:10
  • Have a look at DocumentFile::deleteFile(). – greenapps Nov 21 '16 at 20:42
  • Possible duplicate of [Deleting files via a 'ContentResolver' as opposed to deleting them via 'file.delete()'](http://stackoverflow.com/questions/10925196/deleting-files-via-a-contentresolver-as-opposed-to-deleting-them-via-file-del) – M. Tarek Nov 21 '16 at 20:47
  • @CommonsWare That is the hard part, the uri can come from any app the user uses to pick the file to open. – Twindek Nov 21 '16 at 21:46
  • @M. Tarek Yes basically a duplicate of that. But as I tried getContentResolver().delete() usually does not delete the file, so the answere the is not working for me. – Twindek Nov 21 '16 at 21:47
  • "the uri can come from any app the user uses to pick the file to open" -- no, I mean programmatically. Something in your code is causing you to get this `Uri`. What is it? Part of your [mcve] should be how you get the `Uri` pointing to some content that you wish to delete. – CommonsWare Nov 21 '16 at 21:51
  • @CommonsWare I added the manifest intent filter allowing any app to send the data. Then the "getIntent().getData()" in the activity gets the uri. – Twindek Nov 21 '16 at 22:11

2 Answers2

5

I have solved this using the below code.

DocumentFile.fromSingleUri(getApplicationContext(),fileUri ).delete();

This works fine. Try it.

Tanveer Dayan
  • 496
  • 1
  • 7
  • 18
0

You have no reliable means of deleting this content. It is not your content, and for all you know, it is not technically possible for it to be deleted. Responsibility for deleting it lies with the application that invoked your ACTION_VIEW activity, not you.

If you want to be able to delete the content, invert the flow. On Android 4.4+, you could use ACTION_OPEN_DOCUMENT to allow the user to pick a piece of content. Then, the Uri you get back may allow you to delete it using DocumentFile.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Do you think that the DocumentFile class provides any addition over Context.getContentResolver().delete(getIntent().getData(), null, null); ? Seems like a class to be used on the providers side, not on the receivers. (I have seen apps solving the deletion somehow, deleting files they were provided I was not able to.) – Twindek Nov 21 '16 at 22:19
  • @GáborDemkó: "Do you think that the DocumentFile class provides any addition..." -- only for a `Uri` derived through the Storage Access Framework (e.g., `ACTION_OPEN_DOCUMENT`). A `Uri` that you get by other means, such as your ``, does not have to represent something that can be deleted, by any means. – CommonsWare Nov 21 '16 at 22:22
  • @CommonsWare, 602k. How are you doing it? Six days ago 601k. A thousand points in a week. I have to work for that a year. It cannot only be by extensive informative answers i think. The rich get richer... ;-). – greenapps Nov 22 '16 at 08:26