JavaForAndroid
JavaForAndroid

Reputation: 1179

Glide not working with AppCompatActivity in Android?

I am trying to use glide with an AppCompatActivity. It works well if I use an Activity. But if I am extending from an AppCompatActivity, the image is not loaded (there is only a completely transparent imageview).

Any idea how to fix it? I am using Glide 4.12.0 and this code for loading the image:

Glide.with(activity)
                .load(DOMAIN+this.url)
                .placeholder(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .into(this.view);

}

Activity is of type AppCompatActivity (extending from it). And I need to AppCompatActivity for the Material.io Date picker.

Upvotes: 0

Views: 155

Answers (1)

Kruti Bhatt
Kruti Bhatt

Reputation: 61

remove placeholder image you can try like this:

public class MainActivity extends AppCompatActivity {
    ImageView img;
    Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity = MainActivity.this;
        img = findViewById(R.id.img);
        String url = "https://images.unsplash.com/photo-1598124146163-36819847286d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80";
        Example.setImage(mActivity,img,url);

    }
public class Example extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }
    public static void setImage(Activity activity, ImageView img,String url){
        try{
            Glide.with(activity)
                    .load(url)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(img);
        }catch (Exception e){
            Log.w("TAG",e.toString());
        }

    }
}

add these 2 lines in manifest file

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

android:usesCleartextTraffic="true"

let me know it works or not in my case its working

Upvotes: 1

Related Questions