Hot questions for Using Glide in firebase

Question:

I am attempting to load images from firebase into a recycler adapter and display them

I have attempted two methods to try and solve this problem the first block of code resulted in the error"You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed)"

The second method the application ran but no images were loaded from reading other similar issues I have seen the isAdded() check but I couldn't seem to get that to work the images are being uploaded into a folder in firebase called post_images and the rest of the data associated with the post (Instagram like app with posting) are in firestore

public BlogRecyclerAdapter(List<BlogPost> blog_list) {

    this.blog_list = blog_list;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
    return new ViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    String desc_data = blog_list.get(position).getDesc();
    holder.setDescText(desc_data);

    String image_url = blog_list.get(position).getImage_url();
    holder.setBlogImage(image_url);

}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    private View mView;

    private TextView descView;
    private ImageView blogImageView;
    public Context mContext;

    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

    }

    public void setDescText(String descText) {

        descView = mView.findViewById(R.id.blog_desc);
        descView.setText(descText);

    }

    public void setBlogImage(final String downloadUri) {

        blogImageView = mView.findViewById(R.id.Post_image_view);
        Glide.with(mContext).load(downloadUri).into(blogImageView);


    }}}

Other method attempted

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        mImageStorage = FirebaseStorage.getInstance().getReference();

        final String current_uid = mCurrentUser.getUid();

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
        mUserDatabase.keepSynced(true);

        mUserDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                StorageReference filepath = mImageStorage.child("profile_images").child(current_uid + (".jpeg"));
                Log.d("heere", "S");

                // This gets the download url async
                filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {

                        //The download url
                        final String downloadUrl = uri.toString();
                        Log.d("tag", downloadUrl);
                        if (!downloadUrl.equals("default")) {

                            Glide.with(mContext).load(downloadUrl).into(blogImageView);

                        //    Glide.with(getApplicationContext()).load(downloadUrl).into(mDisplayImage);


                        }
                    }});}

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });}}}

I would like the image to be displayed in the recycler view


Answer:

Change this line

Glide.with(mContext).load(downloadUri).into(blogImageView);

to this instead:

Glide.with(itemView.getContext()).load(downloadUri).into(blogImageView);

All ViewHolder instances have a field itemView that is guaranteed to be non-null, and you can get the context from that.

Question:

I have a problem with loading "Glide" image into "ImageView" with "viewPager".

When I try with "drawable" resource it works fine but for some reason (i guess because it is async) it does not wait for Glide to update the view. Nothing crashes just blank viewPager with 3 screens.

P.S Also, the "Firebase" setup is working fine.

I know that is very hard to replicate a problem with this many libs and stuff but if you can throw some suggestions in the comment section it would be great

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.firebase.ui.storage.images.FirebaseImageLoader;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

public class ScreenSlidePageFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    // Firebase Initial code.
    private FirebaseStorage storage = FirebaseStorage.getInstance();
    private StorageReference storageRef = storage.getReferenceFromUrl("gs://myfirebase_example");
    private StorageReference fileRef = storageRef.child("images/3.jpg");

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_screen_slide_page, container, false);

    Bundle args = getArguments();

    //TODO: Find out how to load the images with Glide, now it is not waiting.
    ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
    //setImageResource(args.getInt(ARG_OBJECT));

    // Getting one image from the DB, it is working perfectly.
    Glide.with(getActivity())
            .using(new FirebaseImageLoader())
            .load(fileRef)
            .into(imageView);

    return rootView;
}
}

Answer:

The problem was missing default image for the ImageView fragment. All is working as expected at the moment.

    <ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srcCompat="@android:drawable/screen_background_dark" />