Android 从 URL 将图像设置为 ImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22008754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Image to a ImageView from URL
提问by IP696
I have Imageview and a link to the picture on the Internet. I set this picture to the ImageView like this,
我有 Imageview 和 Internet 上图片的链接。我将这张图片设置为这样的 ImageView,
public ImageView iv;
iv = (ImageView) findViewById(R.id.imageView);
String img = "https://www.google.com/images/srpr/logo11w.png";
iv.setImageDrawable(Drawable.createFromPath(img));
What I want to do is download a picture from the internet to my android application and apply it to an ImageView. I want to make it as easy as possible.
我想要做的是从互联网下载一张图片到我的 android 应用程序并将其应用到 ImageView。我想让它尽可能简单。
helped me
帮助过我
String img_url= //url of the image
URL url=new URL(img_url);
Bitmap bmp;
bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
ImageView iv=(ImageView)findviewById(R.id.imageview);
iv.setImageBitmap(bmp);
回答by DrChivas
回答by DrChivas
This methodcreate a Drawable from file path name. Checkthis answer.
回答by Mdlc
There are many libraries available on doing this, I recommend using a library, because a they have many extra options availble.
有很多可用的库来做这件事,我建议使用一个库,因为它们有很多额外的选项可用。
使用通用图像加载器
Features
特征
- Multithread image loading
- Possibility of wide tuning ImageLoader's configuration (thread executors, downloader, decoder, memory and disc cache, display image options, and others)
- Possibility of image caching in memory and/or on device's file system (or SD card)
- Possibility to "listen" loading process
- Possibility to customize every display image call with separated options Widget support
- Possibility to show an custom Image on loading, error, etc.
- 多线程图片加载
- 可以广泛调整 ImageLoader 的配置(线程执行器、下载器、解码器、内存和磁盘缓存、显示图像选项等)
- 可以在内存和/或设备的文件系统(或 SD 卡)中缓存图像
- 可以“听”加载过程
- 可以使用单独的选项自定义每个显示图像调用小部件支持
- 可以在加载、错误等时显示自定义图像。
You have to set the options one time using:
您必须使用以下方法一次设置选项:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config);
Then, lateron you can use this anywhere in your code:
然后,稍后您可以在代码中的任何位置使用它:
ImageView iv = (ImageView) findViewById(R.id.imageView);
String str = "http://google.com/img.jpg";
ImageLoader.getInstance().displayImage(str, iv);
Ofcourse there are many other libraries you can use like picasso, Smart Image View, Url Image Viewand many others.
当然,您还可以使用许多其他库,例如picasso、Smart Image View、Url Image View和许多其他库。