I know the absolute path of an image (say for eg, /sdcard/cats.jpg). Is there any way to the content uri for this file ?
Actually in my code I download an image and save it at a particular location. In order to set the image in an ImageView currently I open the file using the path, get the bytes and create a bitmap and then set the bitmap in the ImageView. This is a very slow process, instead if I could get the content uri then I could very easily use the method ImageView.setImageUri(uri)
// This code works for images on 2.2, not sure if any other media types
Try with:
Or with:
UPDATE
I had same question for my file explorer activity. You should know that the contenturi for file only supports mediastore data like image, audio and video. I am giving you the code for getting image content uri from selecting an image from sdcard. Try this code, maybe it will work for you...
Obtaining the File ID without writing any code, just with adb shell CLI commands:
The accepted solution is probably the best bet for your purposes, but to actually answer the question in the subject line:
In my app, I have to get the path from URIs and get the URI from paths. The former:
The latter (which I do for videos, but can also be used for Audio or Files or other types of stored content by substituting MediaStore.Audio (etc) for MediaStore.Video):
Basically, the
DATA
column ofMediaStore
(or whichever sub-section of it you're querying) stores the file path, so you use that info to look it up.U can try below code snippet