31

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&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

but there is no option to set url

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
Shanaz K
  • 678
  • 2
  • 13
  • 28

11 Answers11

67

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.

Eric Schmidt
  • 312
  • 1
  • 11
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • In 1st method, instead of setting imageView directly from onPostExecute, use imageView.post(new Runnable() { public void run() { imageView.setImageBitmap(result); } }); – Tushar Nallan Jan 31 '15 at 15:10
  • @Tushar its not needed..onpostExecute() will call in UI thread. – kalyan pvs Feb 02 '15 at 04:36
  • Eres un crack, te mereces todo Jajajaj – FuriosoJack Sep 19 '17 at 16:10
  • For **Direct Method** you need to add this Code in `onCreate` method, Code Mentioned bellow `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` – S Kumar Apr 17 '20 at 15:15
48

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

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

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

Prashant
  • 394
  • 1
  • 6
  • 18
ehnmark
  • 2,656
  • 3
  • 24
  • 20
  • But it work for .png type image url, it didn't worked for .svg url or for any other url to me . If there is any work around kindly share !!! – Tarit Ray Jul 11 '18 at 10:02
9

Try:

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

More from

1) how-to-load-an-imageview-by-url-in-android.

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

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
8

Using Glide library:

Glide.with(context)
  .load(new File(url)
  .diskCacheStrategy(DiskCacheStrategy.ALL)
  .into(imageView);
Pang
  • 9,564
  • 146
  • 81
  • 122
Priyanka
  • 381
  • 1
  • 6
  • 8
5

You can use either Picasso or Glide.

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


Glide.with(context)
   .load(your_url)
   .into(imageView);
Prashasth Nair
  • 167
  • 2
  • 12
1

easy way to use Aquery library it helps to get direct load image from url

AQuery aq=new AQuery(this); // intsialze aquery
 aq.id(R.id.ImageView).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
Hussein Alrubaye
  • 915
  • 8
  • 14
1

Try the library SimpleDraweeView

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

and now you can simply do:

 final Uri uri = Uri.parse(post.getImageUrl());
HannahCarney
  • 3,441
  • 2
  • 26
  • 32
1

For one-liner programmers,

imageView.setImageBitmap(BitmapFactory.decodeStream(new URL("https://www.vectortemplates.com/raster/batman-logo-big.gif").openConnection().getInputStream()));
ʀᴀʜɪʟ
  • 235
  • 1
  • 4
  • 8
0

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);
}
Andres Felipe
  • 4,292
  • 1
  • 24
  • 41
0

With the latest version of Picasso (2.71828 at the time of writing this answer), the with method has been deprecated.

So the correct way would be-

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

where imageView is the ImageView you want to load your image into.

Manish Paul
  • 171
  • 1
  • 5
-2

Try this :

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

You can also try this : Android-Universal-Image-Loader for efficiently loading your image from URL

Harish Godara
  • 2,388
  • 1
  • 14
  • 28