gpt4 book ai didi

java - 如何在 Picasso 的帮助下获取存储在 Firebase 数据库中的图像

转载 作者:行者123 更新时间:2023-12-02 01:18:44 24 4
gpt4 key购买 nike

我正在尝试编写一个可以发布一些照片和文本的应用程序。我可以从 Firebase 数据库获取文本,但是我无法访问存储在 Firebase 存储部分中的图像(我也将它们添加到数据库中)。图像无法加载,当我检查 logcat 时, picasso 说

Unrecognized type of request: Request{com.google.android.gms.tasks.zzu@78d7d49}

所以我无法给出图像的下载网址,因为图像作为字符串存储在数据库中,如 com.google.android.gms.tasks.zzu@78d7d49

我如何访问它们?

 public static class BlogViewHolder extends RecyclerView.ViewHolder {

View mView;

public BlogViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}

public void setTitle(String title) {
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}

public void setDesc(String desc) {
TextView post_text = (TextView) mView.findViewById(R.id.post_text);
post_text.setText(desc);
}

public void setImage(Context ctx, String image) {
final ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
// Glide.with(ctx).load(image).into(post_image);

Picasso.get().setLoggingEnabled(true);
Picasso.get().load(image).into(post_image);

// StorageReference load = FirebaseStorage.getInstance().getReference().child("Images").child(image);
// load.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
// @Override
// public void onSuccess(Uri uri) {
// Picasso.get().load(uri.toString()).into(post_image);
// }
// });



}
}
 @Override
protected void onStart() {
super.onStart();

FirebaseRecyclerOptions<Images> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Images>()
.setQuery(mData, Images.class)
.build();

FirebaseRecyclerAdapter<Images, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Images, BlogViewHolder>(firebaseRecyclerOptions) {



@Override
protected void onBindViewHolder(@NonNull BlogViewHolder blogViewHolder, int i, @NonNull Images blog) {
blogViewHolder.setTitle(blog.getTitle());
blogViewHolder.setDesc(blog.getDesc());
blogViewHolder.setImage(getApplicationContext(), blog.getImage());
}

@NonNull
@Override
public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_row, parent, false);
return new BlogViewHolder(view);
}
};

firebaseRecyclerAdapter.startListening();
mMainList.setAdapter(firebaseRecyclerAdapter);

}
public class Images {

private String desc;
private String image;
private String title;

public Images(String desc, String image, String title) {
this.desc = desc;
this.image = image;
this.title = title;
}

public Images() {

}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

最佳答案

确保您的图片的网址格式类似于“https://firebasestorage.googleapis.com/v0/b/burnab-812eb.appspot.com/o/users%2Fprofile_images%2Fzyp599FGT5NJvTSC0YbKJJRWjqX2.jpg?alt=media&token=8b7a1399-9217-40dd-8a05-49f9b7b7420f

这样 picasso 就可以从数据库中获取图像。

如果您没有存储中的下载网址。那么你可以在下面实现这一点

final StorageReference childRef = storageReference.child("posts/" + user_id + "/" + ts + ".jpg");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 18, baos);
byte[] thumbData = baos.toByteArray();


final UploadTask thumb_uploadTask = thumb_childRef.putBytes(thumbData);

thumb_uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Task<Uri> thumb_uriTask = thumb_uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return thumb_childRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {

Uri downloadUri = task.getResult();

final String mUri = downloadUri.toString();
final HashMap<Object, Object> dataMap = new HashMap<>();
dataMap.put("profile_image", profile_image);

if (bundle != null) {
group_id = bundle.getString("group_id");
dataMap.put("group_id", group_id);
mDatabase.child("group").child(group_id).child("posts").push().setValue(dataMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Post added successfully", Toast.LENGTH_LONG).show();
onBackPressed();
}
}
});
}
}
});
}

}
});

关于java - 如何在 Picasso 的帮助下获取存储在 Firebase 数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639028/

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