Hot questions for Using Glide in android asynctask

Top 10 Java Open Source / Glide / android asynctask

Question:

It is throwing error

Caused by: java.lang.IllegalArgumentException: You must call this method on the main thread

Class CacheClearAsyncTask extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        Glide.get(getActivity()).clearDiskCache();
        Glide.get(getActivity()).clearMemory();

        return null;
    }

    @Override
    protected void onPostExecute (Void result)
    {
        //Toast.makeText(getActivity(), "Cache cleared", Toast.LENGTH_LONG).show();
    }
}

Setting Prefference on click event

clearCacheBtnPref=findPreference(getResources().getString(R.string.pref_btn_clear_cache_key));
    clearCacheBtnPref.setOnPreferenceClickListener(new 

Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    new CacheClearAsyncTask().execute();
                    return true;
                }
            });

This can not be called in main thread as it is also throws errors and also it does not let me use asynctask.

    Glide.get(getActivity()).clearDiskCache();
    Glide.get(getActivity()).clearMemory();

Answer:

clearDiskCache() must be called from background thread while clearMemory() from main thread so:

class CacheClearAsyncTask extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        Glide.get(getActivity()).clearDiskCache();    
        return null;
    }

    @Override
    protected void onPostExecute (Void result)    {
        Glide.get(getActivity()).clearMemory();
    }
}

Question:

I'm programming an app like Tinder. In my activity I initialize an arraylist which contains all possible matches. And then in my adapter I used to load the profile image from the user in the ImageView with glide.with(context).load(url).into(ImageView), this also works perfectly however my onClickListener in my adapter does not work. Apparently the problem is that the main thread is "frozen" because he has too much work to do with loading the images in the image view. Because of that, I implemented an AsyncTask where I download the image in the background and in onPostExecute I load it in the ImageView. This also works, however when I swiped 4/5 users, suddenly an image from another user is displayed for the wrong user and if I wait some seconds, the ImageView is somehow "updated" and the correct ImageView appears.

I have no idea why I have this behaviour and I thought that Maybe loading images with bitmap is too slow and if I could download the image from the web in the background and load it with glide in onPostExecute it would work? If anyone has an idea I would really appreciate it!

    private List<cards> listItems;
    private Context context;


    public arrayAdapter(List<cards> listItems ,Context context){
        this.listItems = listItems;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        final cards currentItem = listItems.get(i);

        viewHolder.name.setText(currentItem.getName());
        viewHolder.comment.setText(currentItem.getComment());

        Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);

        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!currentItem.getProfileImageUrl2().equals("default")){
                    Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewHolder.image);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView name, comment;
        public ImageView image;
        private CardView card;
        private String imageUrl;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            image = (ImageView) itemView.findViewById(R.id.image);
            comment = (TextView) itemView.findViewById(R.id.commentText);
            card = (CardView) itemView.findViewById(R.id.card);
            }
    }



}





Answer:

Instead of dealing with AsyncTasks you can just use Picasso library, give the image url and the imageView you want and it will do the magic.

Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

For details check here:

https://square.github.io/picasso/