27

我想使用 Url 在 ImageView 中设置图像,例如我有这个 url

http://www.google.iq/imgres?hl=en&biw=1366&bih=667&tbm=isch&tbnid=HjzjsaANDXVR9M:&imgrefurl=http://www.vectortemplates.com/raster-batman.php&docid=FxbVmggVf--0dM&imgurl=http:// /www.vectortemplates.com/raster/batman-logo-big.gif&w=2072&h=1225&ei=Zeo_UoSWIMaR0AXl_YHIBg&zoom=1

但没有设置网址的选项

4

11 回答 11

65

编辑:

创建一个扩展AsyncTask的类

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);
    }

}

并称之为new ImageLoadTask(url, imageView).execute();

直接法:

使用此方法并将您的 url 作为字符串传递。它返回一个位图。将位图设置为您的 ImageView。

public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

然后像这样到 ImageView :

imageView.setImageBitmap(getBitmapFromURL(url));

并且不要忘记 maifest 中的这个权限。

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

笔记:

尝试从另一个线程或 AsyncTask 调用此方法,因为我们正在执行网络操作。

于 2013-09-23T07:19:12.413 回答
45

您还可以让 Square 的Picasso库完成繁重的工作:

Picasso
    .get()
    .load("http://...")
    .into(imageView);

作为奖励,您可以获得缓存、转换等。

于 2015-07-22T12:15:30.277 回答
9

尝试:

URL newurl = new URL(photo_url_str); 
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
profile_photo.setImageBitmap(mIcon_val);

更多来自

1)如何通过 url-in-android 加载图像视图

2) android-make-an-image-at-a-url-equal-to-imageviews-image

于 2013-09-23T07:18:44.633 回答
8

使用Glide库:

Glide.with(context)
  .load(new File(url)
  .diskCacheStrategy(DiskCacheStrategy.ALL)
  .into(imageView);
于 2017-05-17T06:11:20.377 回答
4

您可以使用PicassoGlide

Picasso.get()
   .load(your_url)
   .into(imageView);


Glide.with(context)
   .load(your_url)
   .into(imageView);
于 2020-04-23T04:15:21.573 回答
1

尝试库 SimpleDraweeView

<com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/badge_image"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

现在你可以简单地做:

 final Uri uri = Uri.parse(post.getImageUrl());
于 2017-03-23T17:00:34.400 回答
1

使用Aquery库的简单方法它有助于从 url 获取直接加载图像

AQuery aq=new AQuery(this); // intsialze aquery
 aq.id(R.id.ImageView).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
于 2015-11-04T13:14:52.720 回答
0

使用最新版本的毕加索(撰写此答案时为 2.71828),该with方法已被弃用。

所以正确的方法是——

Picasso.get().load("https://<image-url>").into(imageView);

您要将图像加载到imageViewImageView在哪里。

于 2020-07-28T09:16:34.113 回答
0

如果您正在制作 RecyclerView 并使用适配器,那么对我有用的是:

@Override
public void onBindViewHolder(ADAPTERVIEWHOLDER holder, int position) {
    MODEL model = LIST.get(position);
    holder.TEXTVIEW.setText(service.getTitle());
    holder.TEXTVIEW.setText(service.getDesc());

    Context context = holder.IMAGEVIEW.getContext();
    Picasso.with(context).load(model.getImage()).into(holder.IMAGEVIEW);
}
于 2017-08-02T16:52:22.520 回答
0

对于单行程序员,

imageView.setImageBitmap(BitmapFactory.decodeStream(new URL("https://www.vectortemplates.com/raster/batman-logo-big.gif").openConnection().getInputStream()));
于 2022-01-07T05:57:46.680 回答
-2

尝试这个 :

ImageView imageview = (ImageView)findViewById(R.id.your_imageview_id);
Bitmap bmp = BitmapFactory.decodeFile(new java.net.URL(your_url).openStream());  
imageview.setImageBitmap(bmp);

你也可以试试这个:Android-Universal-Image-Loader用于有效地从URL

于 2013-09-23T07:19:12.900 回答