如何在 Picasso 的帮助下获取存储在 Firebase 数据库中的图像
How can I get the image that stored in Firebase Database with the help of Picasso
我正在尝试编写一个应用程序,您可以 post 一些照片和文本。我可以从 firebase 数据库获取文本,但是我无法访问存储在 Firebase 的存储部分中的图像(我还将它们添加到数据库中)。图片无法加载,当我检查 logcat 时,Picasso 说
Unrecognized type of request: Request{com.google.android.gms.tasks.zzu@78d7d49}
所以我无法提供图像的下载 url 因为图像是以字符串形式存储在数据库中的 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;
}
}
这样 Picasso 就可以从数据库中获取图像了。
如果您没有从存储中下载 url。那么你可以在下面实现
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();
}
}
});
}
}
});
}
}
});
我正在尝试编写一个应用程序,您可以 post 一些照片和文本。我可以从 firebase 数据库获取文本,但是我无法访问存储在 Firebase 的存储部分中的图像(我还将它们添加到数据库中)。图片无法加载,当我检查 logcat 时,Picasso 说
Unrecognized type of request: Request{com.google.android.gms.tasks.zzu@78d7d49}
所以我无法提供图像的下载 url 因为图像是以字符串形式存储在数据库中的 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;
}
}
这样 Picasso 就可以从数据库中获取图像了。
如果您没有从存储中下载 url。那么你可以在下面实现
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();
}
}
});
}
}
});
}
}
});