ImageView.setImageURI(Uri uri)可以用于远程文件吗?

10

使用ImageView.setImageURI(Uri uri)从远程服务器加载图像是否可行?


1
你还没尝试过吗?我一直在使用这种方法。编辑:这里是另一个很酷的例子。 - Macarse
3
顺便提一下,它是ImageView.setImageURI(Uri uri),而不是URI,这之间有很大的区别。 - Felix
2个回答

12

简短的答案是不行!无法实现。

例如,如果uri包含对本地文件的引用,您可以使用ImageView.setImageURI(Uri uri)。 例如:file:///sdcard/images/thumb.png


7

要从目录加载图像,它应该首先转换为Drawable。这是一段可以帮助的代码:

File file = new File ("/sdcard/1.jpg");

ImageView imageView = (ImageView) findViewById(R.id.icon);

imageView.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));

请注意,ImageView 还有另一种方法 setImageURI(URI uri)。该方法用于加载外部文件,不适用于类型为 File 的情况。例如,下面的代码就无法工作:

File file = new File ("/sdcard/1.jpg");

ImageView imageView = (ImageView) findViewById(R.id.icon);

imageView.setImageURI(Uri.fromFile(file));

感谢Martin Wibbels在这篇文章中的分享。


那个最新的代码示例对我来说确实可行(API级别为15左右)。也许这是一个新事物。但也要注意,setImageURI()的参数是Uri而不是URI - LarsH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接