how to set image from url for imageView

后端 未结 10 1753
礼貌的吻别
礼貌的吻别 2020-11-28 08:18

I wanna set Image in ImageView using Url for example I have this url

http://www.google.iq/imgres?hl=en&biw=1366&bih=667&tbm=isch&tbni

相关标签:
10条回答
  • 2020-11-28 08:51

    You can also let Square's Picasso library do the heavy lifting:

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

    As a bonus, you get caching, transformations, and more.

    0 讨论(0)
  • 2020-11-28 08:52

    You can use either Picasso or Glide.

    Picasso.with(context)
       .load(your_url)
       .into(imageView);
    
    
    Glide.with(context)
       .load(your_url)
       .into(imageView);
    
    0 讨论(0)
  • 2020-11-28 08:54

    EDIT:

    Create a class that extends 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);
        }
    
    }
    

    And call this like new ImageLoadTask(url, imageView).execute();

    Direct method:

    Use this method and pass your url as string. It returns a bitmap. Set the bitmap to your 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;
        }
    }
    

    And then this to ImageView like so:

    imageView.setImageBitmap(getBitmapFromURL(url));
    

    And dont forget about this permission in maifest.

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

    NOTE:

    Try to call this method from another thread or AsyncTask because we are performing networking operations.

    0 讨论(0)
  • 2020-11-28 08:54

    if you are making a RecyclerView and using an adapter, what worked for me was:

    @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);
    }
    
    0 讨论(0)
提交回复
热议问题