Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 280
mAx Avatar asked Oct 08 '15 10:10

mAx


2 Answers

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).

like image 93
zmarties Avatar answered Sep 28 '22 04:09

zmarties


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.

like image 31
mchouhan_google Avatar answered Sep 28 '22 04:09

mchouhan_google