Android的ImageView的 - 从URL加载图像 [英] Android ImageView - Load Image from URL

查看:527
本文介绍了Android的ImageView的 - 从URL加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些接触与他们有联系的IMAGEURL String对象的每个。所有我见过的把图像转换成一个ListView的方法涉及手动把图像变成了绘制的文件夹,并调用资源。手动输入的图像会破坏这样做的目的。我提供了我的getView方法和注释掉线是一个我感到困惑。

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
    LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看排= inflater.inflate(R.layout.single_row,父母,假);
    TextView的名称=(TextView中)row.findViewById(R.id.topLine);
    TextView的手机=(TextView中)row.findViewById(R.id.secondLine);
    ImageView的图标=(ImageView的)row.findViewById(R.id.icon);

    name.setText(contactArray.get(位置).getName());
    phone.setText((CharSequence的)contactArray.get(位置).getPhone()getWorkPhone());
    //icon.setImage从contactArray.get(位置).getImageURL(); ????

    返回行;
}
 

当使用ListView控件,你应该异步加载图像,否则你的看法会冻结,案件的ANR

解决方案

。以下是这将异步加载图像完整的code例子。
您的自定义适配器内部创建这个类。

 类ImageDownloader扩展的AsyncTask<字符串,太虚,位图> {
  ImageView的bmImage;

  公共ImageDownloader(ImageView的bmImage){
      this.bmImage = bmImage;
  }

  受保护的位图doInBackground(字符串...网址){
      字符串URL =网址[0];
      位图米康= NULL;
      尝试 {
        InputStream的时间=新的java.net.URL(URL).openStream();
        米康= BitmapFactory.de codeStream(中);
      }赶上(例外五){
          Log.e(错误,e.getMessage());
      }
      返回米康;
  }

  保护无效onPostExecute(位图的结果){
      bmImage.setImageBitmap(结果);
  }
}
 

现在你可以非常容易地加载图像像下面。

 新ImageDownloader(ImageView的).execute(图像URL会在这里);
 

不要忘了添加以下权限到项目的Manifest.xml文件

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

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.

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;
}

解决方案

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.

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

<uses-permission android:name="android.permission.INTERNET" />

这篇关于Android的ImageView的 - 从URL加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆