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 :)
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 :)
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
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");
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,
load = Url of an image you want to display.
placeholder = Image/Drawable you want to show till your image loads.
error = Image/Drawable you want to show if your url image contains error.
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/