Content Uri vs. File Uri: What to use in the future?
Asked Answered
M

2

8

I am developing for an android camera app and we used uris in several scenarios e.g. delivering result data from our camera back to the app that started us for result or when an IntentChooser is opened when the user clicks on the share button.

For now we used File Uris in most cases, because it seemed to be the safer solution. Working with the ContentResolver and the MediaStore there's always something that can go wrong. Furthermore I remember that there were quite a few apps some time ago, that did not support Content Uris, but File Uris very well.

However, we now got mailed by Google that delivering with File Uris does not allow their 'Hangouts' app to access the file, when the user decided not to give 'Hangouts' file access permissions on Android 6.0.

My question now is: Should we generally switch from File Uris to Content Uris when delivering content to other apps? Is that the way to go and should every app rely on / support Content Uris?

Makings answered 8/10, 2015 at 10:42 Comment(1)
commonsware.com/blog/2015/10/07/…Embryogeny
F
5

Yes, content Url's are the way to go.

In the case of a camera app, I'm not sure that file urls were ever very sensible - having passed a file to the calling app, how do you know when you can delete the file? With a content url, the calling app requests the data via the url, and when you have delivered it, you can free your copy of the resources. (If the app wants "permanent" access to the data accessed via the content url, then it is up to it to save that data itself).

Facile answered 8/10, 2015 at 11:6 Comment(3)
so there is some kind of agreement, that content uris are highly temporary and file uris aren't? Otherwise, how am I supposed to know when the recipient of my Content Uri is done using it?Makings
All urls are pretty much temporary - if you want to guarantee you get the same data from a url then you cache that data, rather than request it multiple times. In the case of file urls, since they simply point at the file people end up using them without informing you (as the provider) that they have used them, so you don't have a good trigger as to when you can delete the underlying file (and often the users are lazy, and consider the file as being a good enough cashed copy of the data).Facile
Alright... that sounds like it is possible for the provider to determine when a content uri he provided to another app was used. Can you please give ma a hint how to do that? Thanks for your effort!Makings
P
4

You should be using content uri to exchange files between applications.Starting with Android 7.0, you will get FileUriExposedException on passing file uri outside your application. Here is an excerpt from behaviour changes starting android 7.0

For apps targeting Android 7.0, the Android framework enforces the StrictMode API policy that prohibits exposing file:// URIs outside your app. If an intent containing a file URI leaves your app, the app fails with a FileUriExposedException exception.

To share files between applications, you should send a content:// URI and grant a temporary access permission on the URI.

Polygamous answered 15/9, 2017 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.