【问题标题】:Retrieve images in recyclerview from Firebase Firestore从 Firebase Firestore 检索 recyclerview 中的图像
【发布时间】:2020-01-06 01:33:04
【问题描述】:

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

不幸的是,我看过类似的问题,但没有任何帮助。

列表活动:

    //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);

}

请看下面的图片链接,谢谢

【问题讨论】:

  • 能把图片和数据库结构的url发一下吗?
  • 请将您的数据库结构添加为屏幕截图。也请回复@AlexMamo
  • @Ashish 请看上面的截图
  • @Neha 您确定您提供的图片网址在您的存储中的图片位于 Firebase 数据库中显示的同一位置之前无法使用。
  • 是的,我知道这是安卓设备中的位置,只是问她为什么不做doc.getString("Img"); 来检索它

标签: android firebase android-recyclerview google-cloud-firestore data-retrieval


【解决方案1】:

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

但请确保在单击任何按钮时运行 Fileuploader。或者如果你的edittext为空,它会抛出空指针异常。

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);
}

【讨论】:

  • 更新上述代码后的数据库结构请看图
【解决方案2】:

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

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

在这个网站上给出了最好的提示和教程。请按照步骤进行操作...

【讨论】:

    【解决方案3】:

    嘿,我认为这可能会使用毕加索

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

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多