How to Set Image from Url for Imageview

How to load an ImageView by URL in Android?

Anyway people ask my comment to post it as answer. i am posting.

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

Android how to set image to imageview from url

try this

1.you can user Glide library to load image from url look the below code it can help you in simple way

compile this library

compile 'com.github.bumptech.glide:glide:4.0.0-RC0'

than load image like this

Glide.with(HomeClass.this)
.load(url)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.dontAnimate()
.into(imageview);

2 .try this if you dont want to use third party library

 new DownloadImage(imamgeview).execute(url);

create a Async Task

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImage(ImageView bmImage) {
this.bmImage = (ImageView ) 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.d("Error", e.getStackTrace().toString());

}
return mIcon11;
}

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

i hope that it will work in your case

Android : How to set an image to an imageview from a url programatically

The easiest way to do it is by using something like Picasso or Glide:

Picasso.with(getContext()).load(imgUrl).fit().into(contentImageView);

you can add picasso library in your gradle:
compile 'com.squareup.picasso:picasso:2.5.2'

I want to show a picture from URL in my recyclerview

You can use Picasso library for the same.

Permission in android manifest

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

Add Picasso in app level build.gradle

implementation 'com.squareup.picasso:picasso:2.5.2'

Loading image

Picasso.get().load(model.getTrack().getAlbum().getImages().get(position).getUrl()).into(holder.songCover);

How to load Image into ImageView from Url using Glide v4.0.0RC1

If you are using Glide v4.0.0-RC1 then you need to use RequestOptions to add the placeholder, error image and other option. Here is an working example

RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round)
.error(R.mipmap.ic_launcher_round);

Glide.with(this).load(image_url).apply(options).into(imageView);

How to set image url as background of an ImageView using data binding?

Try this XML:

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/image_view_background"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="@color/colorPrimary"
android:alpha="0.4"
app:mainImageUrl="@{item.backgroundImageUrl}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<ImageView
android:id="@+id/image_view_main"
android:layout_width="230dp"
android:layout_height="230dp"
app:mainImageUrl="@{item.mainImageUrl}"
android:background="@color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

For checking purpose I just set the alpha for the background image.

Hope this trick will help you.

Sample Image



Related Topics



Leave a reply



Submit