从 Firebase Firestore 中检索 recyclerview 中的图像

Retrieve images in recyclerview from Firebase Firestore

我正在尝试从 Firebase Firestore 检索图像。我能够在 RecyclerView 中成功检索文本,但是我不确定如何检索图像。

不幸的是我看了类似的问题none帮助了。

列表活动:

    //initialize firestore
    db = FirebaseFirestore.getInstance();

    //initialize views
    mRecyclerView = findViewById(R.id.resultRecycle);
    //set recycler view properties
    mRecyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);

    //show  data in recycler view
    showData();
}

private void showData() {

    db.collection("Item")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    //called when data is retrieved
                    //show data
            for(DocumentSnapshot doc: task.getResult()){

                        Model model = new 
                    Model(doc.getString("id"),
                                doc.getString("Title"),
                                doc.getString("Location"),
                                //STUCK HERE
                        );


                        modelList.add(model);
                    }

                    //adapter
                    adapter = new CustomAdapter(ListActivity.this, modelList);
                    //set adapter to recycler view
                    mRecyclerView.setAdapter(adapter);

                }
            });

自定义适配器:

public void onItemClick(View view, int position) {
            //this will be called when user clicks an item

            //show data on toast when clicking
            String title = modelList.get(position).getTitle();
            String location = modelList.get(position).getLocation();
            String  url = modelList.get(position).getUrl();
        }

        @Override
        public void onItemLongClick(View view, int position) {
            //this will be called when user clicks long item
        }
    });

    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
    //bind views /set data
    holder.mTitle.setText(modelList.get(i).getTitle());
    holder.mLocation.setText(modelList.get(i).getLocation());
    Picasso.get().load(modelList.get(i).getUrl()).into(holder.mUrl);

}

请看下图link,谢谢

使用 fileuploader 代码将图片上传到 Firebase 数据库中。

但请确保将文件上传器 运行 放在点击任何按钮上。或者,如果您的编辑文本为空,它将抛出空指针异常。

private void Fileuploader(){ 
    if(imguri != null ) { 
        StorageReference ref = mStorageRef.child("Item"); 
        Toast.makeText(UploadActivity.this, "Upload in progress", Toast.LENGTH_LONG).show(); 
        ref.putFile(imguri) 
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
                @Override 
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
                        Toast.makeText(UploadActivity.this, "Image uploaded successfully!", Toast.LENGTH_LONG).show(); 
                        Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
                        while (!urlTask.isSuccessful());
                        Uri downloadUrl = urlTask.getResult();
                        saveData(downloadUrl); 
                    } 
                }) 
            .addOnFailureListener(new OnFailureListener() { 
                @Override 
                    public void onFailure(@NonNull Exception exception) { 
                        Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show(); 
                    } 
            }); 
    } 
}

文件上传成功后,该功能将自动启动。

private void saveData(Uri imguri) { 
    Map<String, String> data = new HashMap<>(); 
    EditText title = findViewById(R.id.etv_1); 
    EditText shortDesc = findViewById(R.id.etv_2); 
    TextView location = findViewById(R.id.etv_3); 
    String eTitle = title.getText().toString(); 
    String eShortDesc = shortDesc.getText().toString(); 
    String eLocation = location.getText().toString(); 
    String eUrl = imguri.toString(); 
    userid = FirebaseAuth.getInstance().getUid(); 
    getCate(); 
    data.put("Img", eUrl); 
    data.put("Title", eTitle); 
    data.put("Location", eLocation); 
    data.put("Short Description", eShortDesc); 
    data.put("Category", cate ); 
    data.put("id", userid); 
    db.collection("Item").add(data); 
    Toast.makeText(this, "Successfully saved your item", Toast.LENGTH_SHORT).show(); 
    Intent i = new Intent(this, Home.class); 
    startActivity(i);
}

我有一个从 Firebase Firestore 在 recyclerview 中检索图像的最佳示例..link 在这里...

https://www.simplifiedcoding.net/firebase-storage-example/

在本网站中提供了最佳提示和教程。请按照步骤操作...

嘿,我认为这可能有助于使用 Picasso

Picasso.with(context).load(ImageURL).into(imageView);