1

I'm having trouble getting file paths from uri returned by other activities.

I found many solutions on how to convert Uri to absolute file path like this one but its always dependent on type of content such as audio in the given link.

My app provides an activity to send files of any type.. that said I set the intent filter to

mimetype  */*

but when I go in file browser and select a image file from from the camera photo folder I get path like /external/images/media/288 but If I pick a music file I get Uri like external/audio/media/1336

but on the other hand if I choose a non media file type (ex.pdf or zip) then I get the absoute path already in the Uri

So as you can see Its not known what path will Uri have it depends on what file i pick for sending with my app.

one possible solution is to make cursor queries for all types of media e.g audio video and image and if they return null then check for Uri.getPath() but I think thats a very bad practice or inefficient way !

Is there any way to get absolute Path from any Uri (given the file exists) without checking for each type of uri?

Community
  • 1
  • 1
Allahjane
  • 1,920
  • 3
  • 24
  • 42
  • http://developer.android.com/guide/topics/providers/document-provider.html#open has two examples how to get to the content of any uri resource. See the inputstream and bitmap example. – zapl Jan 24 '14 at 16:48

1 Answers1

1

I'm having trouble getting file paths from uri returned by other activities.

There does not have to be a "file path" associated with a Uri. A Uri points to a source of data, which may or may not be a file, let alone a file which you can access directly.

You can obtain an InputStream on the data associated with a Uri via a call to openInputStream() on a ContentResolver.

Is there any way to get absolute Path from any Uri

Not in any sort of a reliable fashion. Please use the Uri in the way it was intended.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    And what If I know its always a file but I need to get paths from other activities? – Allahjane Jan 25 '14 at 07:02
  • and what if i **need** to save tons of large files, in a directory which user choose? – Hassan Faghihi Jul 23 '18 at 09:08
  • 1
    @deadManN: Integrate [a directory chooser library](https://android-arsenal.com/tag/35?sort=created), if you wish to stick with stuff that you can access via the filesystem. – CommonsWare Jul 23 '18 at 10:44
  • @CommonsWare one thing i think about was to... generate temp location, save files to that location, then copy newly generated file to the content uri location... but i failed... due to copy operation, to content based location, maybe i should use stream reader and writer, due what i read in book today, but not sure :-s – Hassan Faghihi Jul 23 '18 at 11:04