sagar suri
sagar suri

Reputation: 4741

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

I just updated Glide library from v3 to v4 in my application. But Now I am not able to load image from the url. Previously it was working fine with v3.

Here is my Glide code:

Glide.with(context).load(galleryList.get(itemPosition).getImage()).thumbnail(Glide.with(context).load(R.drawable.balls)).apply(options).into(holder.kolamImage);

What is the change in v4? I went through the document but still no help.

Upvotes: 15

Views: 84971

Answers (5)

Bharath
Bharath

Reputation: 287

Glide.with(this)
        .load("url here") // image url
        .placeholder(R.drawable.placeholder) // any placeholder to load at start
        .error(R.drawable.imagenotfound)  // any image in case of error
        .override(200, 200) // resizing
        .centerCrop()     
        .into(imageView);  // imageview object

Upvotes: 8

Pramesh Bhalala
Pramesh Bhalala

Reputation: 283

Below Steps is for load image into imageView from URL :-

create a new Activity like this and load image from given url.

activity_main.xml

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

    <ImageView
        android:id="@+id/myOfferImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    ImageView myOfferImageView;
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        url = "https://image.url"


        myOfferImageView = findViewById(R.id.myOfferImage);
        Glide.with(this).load(url)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_launcher_background)
                .into(myOfferImageView);
    }


}

Upvotes: 4

DragonFire
DragonFire

Reputation: 4102

Make sure you have quotes around your url, with ivProfileImage being your imageview.

Glide.with(mContext)
    .asBitmap()
    .load("https://i2.wp.com/www.siasat.com/wp-content/uploads/2018/03/Rosamund-Pike.jpeg?fit=600%2C421&ssl=1")
    .into(ivProfileImage);

Upvotes: 0

Prakhar Gupta
Prakhar Gupta

Reputation: 131

Glide v4 added a feature of RequestOptions to add placeholder ,error image and to customize image.

RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.your_placeholder_image)
                    .error(R.drawable.your_error_image);

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

Upvotes: 3

Jeffrey
Jeffrey

Reputation: 4650

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

Upvotes: 35

Related Questions