Convert between file path and Content URI?

2,817 views
Skip to first unread message

Tony G

unread,
Oct 19, 2015, 1:42:58 PM10/19/15
to Automate
To play a sound we use a Content URI. I'd prefer to use a file path for a specific application. Essentially I'm using a file abbreviation like "foo", building a file path with that, then I'd like to play a sound with the path. Until I work that out I will need to create a lookup table that translates "foo" to a hard-coded content:// reference. That's not practical for an app that gets used on different devices, and it uses a lot of flow blocks.

I guess the solution to this would be expression functions like pathToUri and uriToPath.The code for this is available in Q&A from Stack Overflow and elsewhere.

Another application for this: The FileExists block checks for the existence of a file based on a path. But we use content URIs to actually use media. So we need a way to translate between the two - or for that and perhaps other File blocks to support a content URI.

Other ideas? Thanks!

Automate developer

unread,
Oct 19, 2015, 2:10:46 PM10/19/15
to Automate
Content URIs aren't always referencing and actual file, so there's no way to convert them.
Use the Content copy block to extract and copy the content to an actual file.

When using the Content pick block, select the "SD card" (with Automate icon) instead of "Internal storage" in the "document" app, as this will return "file://" URIs (direct file system paths).

I'am still working the workaround for accessing files on removable SD cards on Android 5+.

Tony G

unread,
Oct 19, 2015, 2:57:29 PM10/19/15
to Automate
Thanks for the feedback. As I said in the OP, conversion methods between URIs and paths are easily found, and sure, while some URIs don't translate directly to files, it would help to be able to translate those that are expected to exist.

Is there documentation for this syntax? "file://{"downloads";storage}/file.mp3"
Found here: https://groups.google.com/forum/#!topic/automate-user/zYGIIfdpGpM

I'd rather not copy media around. The goal is to play audio based on an ID provided at runtime. This will not be the numeric content URI ID, but the reference will always be for a file which is expected to be on the device. I have been assigning the URI to a variable depending on whether I expect the file to be under "/internal/" or "/external/" but that's awkward.

So with all of that, I might need to create a flow specifically to install desired files under a common folder (created with a File Make Directory block) so that I can provide a list of IDs that are guaranteed to be available on any device using the flow that references them. I'm not adverse to that as long as all blocks can use a file:// URI rather than content://.

As to removable SD, as you know, some devices don't even have a SD slot. What problems might we expect in this area on v5+ when the "/external/" reference is emulated?

Thanks again.

Automate developer

unread,
Oct 19, 2015, 3:29:31 PM10/19/15
to Automate
The linked post is old. If you write a relative path (not starting with a content://) in a content URI field it's automatically normalized to an absolute file URI in on the external storage:
test.txt => /storage/emulated/0/test.txt

Any block taking a content URI can also use an file:// URI, or filename as described above.
Reply all
Reply to author
Forward
0 new messages