Allahjane
Allahjane

Reputation: 1978

How to get absolute path from a variable type Uri

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?

Upvotes: 1

Views: 1113

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006724

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.

Upvotes: 1

Related Questions