gpt4 book ai didi

android - 从 Firebase Firestore 检索 recyclerview 中的图像

转载 作者:行者123 更新时间:2023-12-02 16:56:20 27 4
gpt4 key购买 nike

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

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

列表 Activity :

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

}

请看下图链接,谢谢 enter image description here

最佳答案

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

但请确保单击任何按钮即可运行 Fileuploader。否则,如果您的编辑文本为空,它将抛出 nullpointer 异常。

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

关于android - 从 Firebase Firestore 检索 recyclerview 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57766662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com