How to display images from external URL in List-view on Android

Rashmi
Rashmi
Member
820 Points
17 Posts

I want to display images on list-view which is coming from external URL. I'm trying following:

public static Bitmap getImageBitmap(String url) {
        Bitmap imageBitmap = null;
        try {
            URL imageURL = new URL(url);
            imageBitmap = BitmapFactory.decodeStream(imageURL.openStream());
        } catch (IOException e) {

        }
       return imageBitmap;
    }

And using it in recycler-view adapter as:

public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setText(mValues.get(position).id);
        Bitmap bitmapImg = AppUtility.getImageBitmap(mValues.get(position).imgProfileUrl);
        if (bitmapImg != null)
            holder.mImgProfileView.setImageBitmap(bitmapImg);

But app crashing during load. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imgProfile"
        android:layout_width="50dp"
        android:layout_height="50dp"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
Views: 2018
Total Answered: 1
Total Marked As Answer: 1
Posted On: 07-Dec-2019 08:29

Share:   fb twitter linkedin
Answers
Smith
Smith
None
2568 Points
74 Posts
         

I think, the best library for such a task is Picasso by Square. It allows to load an image to an ImageView by URL with one-liner code:

Picasso.get()
.load(mValues.get(position).imgProfileUrl)
.into(holder.mImgProfileView);

You can find it at Github https://square.github.io/picasso/

Posted On: 09-Dec-2019 07:45
thanks for suggestion.
 - Rashmi  25-Dec-2019 06:43
 Log In to Chat