开发者

Does ImageView.setImageURI(Uri uri) work with remote files?

I开发者_如何学JAVAs it possible to load an image from a remote server using ImageView.setImageURI(Uri uri)?


The short answer is no! It can't.

You could use ImageView.setImageURI(Uri uri) for instance if the uri contains a reference to a local file. Eg: file:///sdcard/images/thumb.png


To load an image from a directory, it should be converted to a Drawable first. Here is a piece of code which can help:

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

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

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

Be warned that there is another method for ImageView called setImageURI(URI uri). This method is used to load external files; it doesn't work with the type File. For example, this code won't work:

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

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

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

Thanks to Martin Wibbels for this post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜