Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image from url

Tags:

android

People also ask

Which library is used to load an image from URL into an ImageView?

UrlImageViewHelper will fill an ImageView with an image that is found at a URL.

How do I open an image URL?

On your computer, go to images.google.com. Search for the image. In Images results, click the image. At the top of your browser, click the address bar to select the entire URL.

What is the URL for an image?

What is an image URL? A URL is a web address that specifies location. Therefore, an image URL is a web address that specifies the location of an image. Having an image URL makes it easy to share.


The accepted answer above is great if you are loading the image based on a button click, however if you are doing it in a new activity it freezes up the UI for a second or two. Looking around I found that a simple asynctask eliminated this problem.

To use an asynctask to add this class at the end of your activity:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
  ImageView bmImage;

  public DownloadImageTask(ImageView bmImage) {
      this.bmImage = bmImage;
  }

  protected Bitmap doInBackground(String... urls) {
      String urldisplay = urls[0];
      Bitmap mIcon11 = null;
      try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
      } catch (Exception e) {
          Log.e("Error", e.getMessage());
          e.printStackTrace();
      }
      return mIcon11;
  }

  protected void onPostExecute(Bitmap result) {
      bmImage.setImageBitmap(result);
  }
}

And call from your onCreate() method using:

new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
        .execute(MY_URL_STRING);

Dont forget to add below permission in your manifest file

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

Works great for me. :)


URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

try picasso nice and finishes in one statement

Picasso.with(context)
                .load(ImageURL)
                .resize(width,height).into(imageView);

tutorial: https://youtu.be/DxRqxsEPc2s

(note: Picasso.with() has been renamed to Picasso.get() in the latest release)


There is two way :

1) Using Glide library This is best way to load image from url because when you try to display same url in second time it will display from catch so improve app performance

Glide.with(context).load("YourUrl").into(imageView);

dependency : implementation 'com.github.bumptech.glide:glide:4.10.0'


2) Using Stream. Here you want to create bitmap from url image

URL url = new URL("YourUrl");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bitmap);