Android ImageView - 从 URL 加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22212463/
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
Android ImageView - Load Image from URL
提问by Xonal
I have a number of "contact" objects each with an imageURL String associated with them. All the ways I've seen of putting images into a ListView involve manually putting images into a "drawable" folder and calling resources. Manually entering the images in would defeat the purpose of this. I've provided my getView method and the commented out line is the one I'm confused about.
我有许多“联系人”对象,每个对象都有一个与之关联的 imageURL 字符串。我见过的将图像放入 ListView 的所有方法都涉及手动将图像放入“可绘制”文件夹并调用资源。手动输入图像会破坏此目的。我提供了我的 getView 方法,注释掉的行是我感到困惑的行。
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
TextView name = (TextView) row.findViewById(R.id.topLine);
TextView phone = (TextView) row.findViewById(R.id.secondLine);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
name.setText(contactArray.get(position).getName());
phone.setText((CharSequence) contactArray.get(position).getPhone().getWorkPhone());
//icon.setImage from contactArray.get(position).getImageURL(); ????
return row;
}
回答by Adnan
While using listView you should load image asynchronously, otherwise your view will be freezes and case an ANR. Following is a complete code example which would load image asynchronously.
Create this class inside your custom adapter.
在使用 listView 时,您应该异步加载图像,否则您的视图将冻结并出现 ANR。以下是一个完整的代码示例,它将异步加载图像。
在您的自定义适配器中创建此类。
class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public ImageDownloader(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String url = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Now you can load the image very easily like following.
现在您可以非常轻松地加载图像,如下所示。
new ImageDownloader(imageView).execute("Image URL will go here");
Don't forget to add following permission into your project's Manifest.xml file
不要忘记在项目的 Manifest.xml 文件中添加以下权限
<uses-permission android:name="android.permission.INTERNET" />
回答by Adnan
Load Image from URL like this.
像这样从 URL 加载图像。
URL url = new URL(contactArray.get(position).getImageURL());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
icon.setImageBitmap(bmp);
Perhaps if you are looking for more comprehensive way and you have very large data set. I would recomend you to use Android-Universal-Image-Loaderlibrary.
也许如果您正在寻找更全面的方法并且您拥有非常大的数据集。我建议您使用Android-Universal-Image-Loader库。

