从android中的文件路径获取内容uri [英] Get content uri from file path in android
问题描述
我知道图像的绝对路径(例如,/sdcard/cats.jpg).有没有办法获得这个文件的内容uri?
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 ?
实际上,在我的代码中,我下载了一个图像并将其保存在特定位置.为了在 ImageView 实例中设置图像,目前我使用路径打开文件,获取字节并创建位图,然后在 ImageView 实例中设置位图.这是一个非常缓慢的过程,相反,如果我可以获得内容 uri,那么我可以非常轻松地使用方法 imageView.setImageUri(uri)
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)
推荐答案
尝试:
ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg")));
或与:
ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString()));
这篇关于从android中的文件路径获取内容uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!