使用ImageView.setImageURI(Uri uri)从远程服务器加载图像是否可行?
使用ImageView.setImageURI(Uri uri)从远程服务器加载图像是否可行?
简短的答案是不行!无法实现。
例如,如果uri包含对本地文件的引用,您可以使用ImageView.setImageURI(Uri uri)。 例如:file:///sdcard/images/thumb.png
要从目录加载图像,它应该首先转换为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在这篇文章中的分享。
setImageURI()
的参数是Uri
而不是URI
。 - LarsH
ImageView.setImageURI(Uri uri)
,而不是URI
,这之间有很大的区别。 - Felix