如何从URL设置imageView图像

时间:2017-03-16 15:40:21

标签: java android icons

我需要将图像从URL设置为imageView。我找到了一些解决方案,但没有解决方案对我有用。

有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:1)

你可以使用Picasso http://square.github.io/picasso/一个很棒的图书馆来将图像从互联网加载到imageview

答案 1 :(得分:0)

我建议使用https://github.com/bumptech/glide。它非常易于使用。

Glide.with(this).load(URL_TO_IMAGE).into(imageView);

但是,不要忘记添加依赖项:

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

答案 2 :(得分:0)

使用滑动库:

在你的gradle中:

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

在您的活动/片段中:

// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  Glide.with(this).load("YOUR URL").into(imageView);
}

// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
  final ImageView myImageView;
  if (recycled == null) {
    myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
  } else {
    myImageView = (ImageView) recycled;
  }

  String url = myUrls.get(position);

  Glide
    .with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .crossFade()
    .into(myImageView);

  return myImageView;
}

答案 3 :(得分:0)

首先,您需要声明此变量:

Bitmap bmp;

然后添加此代码:

String imageUrl = "URL";
                InputStream in = new URL(imageUrl).openStream();
                bmp = BitmapFactory.decodeStream(in);

ImageView img_results_experience = (ImageView)view.findViewById(R.id.img_results_experience);
                    img_id.setImageBitmap(bmp);

对我有用!!

相关问题