Get content uri from file path in android

pankajagarwal picture pankajagarwal · Jun 9, 2010 · Viewed 388.9k times · Source

I know the absolute path of an image (say for eg, /sdcard/cats.jpg). Is there any way to get 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 instance, currently I open the file using the path, get the bytes and create a bitmap and then set the bitmap in the ImageView instance. 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)

Answer

Francesco Laurita picture Francesco Laurita · Jun 9, 2010

Try with:

ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg")));

Or with:

ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString()));