0

I am having trouble getting the absolute file from a URI on my devices.

I am using the getPath method from here https://stackoverflow.com/a/20559175/765269

The Problem is: The returned path is something like /storage/emulated/0/DCIM.. (for the Gallery).

This path does neither exist on my Nexus 4 (Android L) nor my Photon Q (Android 4.4.4). /storage/emulated/legacy/DCIM.. would be correct (the directory 0 does not exist).

root@mako:/storage/emulated # ls -al lrwxrwxrwx root root 2014-10-04 05:35 legacy -> /mnt/shell/emulated/0

My application registers as a share receiver, then uses the extra_stream parameter to obtain the URI(s). Sharing a picture to the Gmail application works fine, so there must be a way..

Edit: If you substitute the 0 with legacy, the paths are correct.

Community
  • 1
  • 1
user765269
  • 311
  • 1
  • 4
  • 12
  • There is no requirement that a `Uri` must point to a file: http://commonsware.com/blog/2014/07/04/uri-not-necessarily-file.html – CommonsWare Oct 08 '14 at 16:31
  • Ok, but that doesn't really help me. – user765269 Oct 08 '14 at 19:30
  • My point is that you should stop trying to get the "real path", as there may not be one. Use `openFileInput()` on `ContentResolver` to get an `InputStream` represented by the `Uri`. – CommonsWare Oct 08 '14 at 20:32
  • Using openInputStream does work, however this requires me to make a local copy of the file. I am passing the file to a native binary, which opens the file read only. – user765269 Oct 09 '14 at 01:12
  • "however this requires me to make a local copy of the file" -- this is no different than what you would have to do if you were downloading the content off of an HTTP server, or an FTP server, or an email server as an attachment, etc. A `ContentProvider` is simply an on-device server for content. – CommonsWare Oct 09 '14 at 10:55
  • @CommonsWare, however, it seems that even Android's ACTION_VIEW intent does not work with a URI from the Storage Access Framework: http://stackoverflow.com/q/30546441/3060087 – Price Jul 09 '15 at 11:56
  • @Price: You cannot take a `Uri` that you get from the Storage Access Framework and pass it to a third-party app and expect it to work reliably. The point behind the Storage Access Framework is to give *your* app access to a bit of storage, not to give *everything* access to that storage. – CommonsWare Jul 09 '15 at 11:59
  • So if the goal is to let another app open a user selected file, would it be better to avoid dealing with SAF's file picker and just use a custom file chooser that returns the file's actual path? – Price Jul 09 '15 at 12:01
  • @Price: Your goal has issues. Just because your app can access a file does not mean that arbitrary other apps can access that file. Just because your app can access a `Uri` does not mean that arbitrary other apps can access that `Uri`. Not only are there security aspects, but there is the app equivalent of "link rot", where your filesystem path or `Uri` becomes invalid. You may wish to ask a separate SO question, where you provide the business case (e.g., "I am writing a recipe app and I want to attach photos of the completed recipe") and solicit input on options for implementations. – CommonsWare Jul 09 '15 at 12:21
  • @CommonsWare, thanks so much! Here is my question: http://stackoverflow.com/q/31318899/3060087 – Price Jul 09 '15 at 13:27

0 Answers0