1

The main activity xml file with ImageView has src. How to link it to an online image(url).

PS: Just starting with Java and Android Studio, so i know it might be a silly question. Any help appreciated :)

  • https://stackoverflow.com/questions/24535924/how-to-get-image-from-url-website-in-imageview-in-android I think you may got your solution. – Rajneesh Shukla Jan 07 '17 at 08:12

3 Answers3

1

You can use Glide Or Picaso Libraries for that .

{ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

Glide.with(this).load("your link of image").into(imageView);}

above is example of glide.

here is the link for library

https://github.com/bumptech/glide

Rv Lalwani
  • 134
  • 7
1

Add a class

    public 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);
}
 }

Then use

            new DownloadImageTask(imageView).execute("url");
Gsingh
  • 91
  • 9
1

Picasso is the best approach to show image into imageview. Use picasso library to display image from web in an ImageView. Add dependency in your build.gradle like this :

    compile 'com.squareup.picasso:picasso:2.5.2'

Now to display an image from web.Write this code to your activity.

 Picasso.with(mActivity)
                    .load(model.getUserPhotoURL())
                    .placeholder(R.drawable.no_user)
                    .error(R.drawable.no_user)
                    .into(holder.imgUser);

Where,

  1. load = Url of an image you want to display.

  2. placeholder = Image/Drawable you want to show till your image loads.

  3. error = Image/Drawable you want to show if your url image contains error.

  4. into = Id of an imageview where you want to display image.

Note: Make sure you have given Internet permission.

Check this link for more detail : http://square.github.io/picasso/

Parin Parikh
  • 385
  • 1
  • 6