简体   繁体   中英

Glide is not loading image from firebase

I am working on a firebase project but I am facing a problem like when I upload an image on firebase I am not getting it in my original apk. I upload an image on firebase and save its URL in the firebase database and then when I load it with the Recycler view and Glide library it stuck on the placeholder image but did not load the original image. Can anyone help me to solve this problem I provided my source code below:-

EventActivity:-

public class EventActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ImageAdapter imageAdapter;

    private DatabaseReference mDatabaseRef;

    private List<Upload> mUploads;

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

        mDatabaseRef = FirebaseDatabase.getInstance().getReference("NotifyImages");


        mRecyclerView = findViewById(R.id.recycler_view);

        mRecyclerView.setHasFixedSize(true);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mUploads = new ArrayList<>();

        mDatabaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Upload upload = postSnapshot.getValue(Upload.class);
                    mUploads.add(upload);
                }

                imageAdapter = new ImageAdapter(EventActivity.this, mUploads);
                mRecyclerView.setAdapter(imageAdapter);
            }

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

            }
        });

    }
}

Upload class:-

public class Upload {
    private String mName;
    private String mImageUri;

    public Upload() {
    }

    public Upload(String name, String ImageUri) {
        if (name.trim().equals("")) {
            name = "No name";

        }


        mName = name
        ;
        mImageUri = ImageUri;

    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getImageUri() {
        return mImageUri;
    }

    public void setImageUri(String ImageUri) {
        mImageUri = ImageUri;
    }

}

Image Adapter:-

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {


    private Context mContext;
    private List<Upload> mUploads;

    public ImageAdapter(Context context, List<Upload> uploads) {
        mContext = context;
        mUploads = uploads;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
        return new ImageViewHolder(v);
    }

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

        Upload uploadCurrent = mUploads.get(position);
        holder.textViewName.setText(uploadCurrent.getName());
//        Glide.with(mContext)
//                .load(uploadCurrent.getImageUri())
//                .centerCrop()
//                .into(holder.imageView);

        Glide.with(mContext)
                .load(uploadCurrent.getImageUri())
                .placeholder(R.mipmap.ic_launcher_round)
                .centerCrop()
                .into(holder.imageView);

    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public ImageView imageView;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            textViewName = itemView.findViewById(R.id.text_view_name);
            imageView = itemView.findViewById(R.id.image_upload);

        }
    }
}

activity_event.xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>


</RelativeLayout>

ImageItem.xml:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text_view_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="80dp"
            android:text="Name"
            android:textColor="@color/ColorRed"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/image_upload"
            android:layout_width="match_parent"
            android:layout_height="300dp"


            />

    </LinearLayout>

</androidx.cardview.widget.CardView>

You can easily copy my code and test it. Please answer me if you find a solution.

Please make changes in your UploadFile() Function :

fireRef.putFile(ImageUri)
    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                }
            } , 5000);

            Toast.makeText(MainActivity.this, "Upload Successfull", Toast.LENGTH_SHORT).show();
            Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
            while (!urlTask.isSuccessful());
            Uri downloadUrl = urlTask.getResult();
            Upload upload = new Upload(mainEditTextNotify.getText().toString().trim(),
                    downloadUrl.toString());
            String uploadId = mDataRef.push().getKey();
            mDataRef.child(uploadId).setValue(upload);

        }
    })

The above code will provide you proper uri of image which you can store inside Database.

It will provide you proper uri which will help to load images in Glide.

But i suggest to store filepath from Firebase Storage. Cause if uri token expires then you need generate uri token and change with old one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM