无法将 String 类型的对象转换为 Bitmap 类型
Can't convert object of type String to type Bitmap
嗯,因为 none 回答了我,所以我再次询问希望有一些有礼貌的人...我正在尝试检索上传到 Firebase 的图像。在数据快照中,它给出了以下错误:Can't convert object of type java.lang.String to type android.graphics.Bitmap
。我尝试了一些方法来解决这个问题,但没有任何改变。这是代码:
这是读取 post 并将其显示在屏幕上的代码。
private void ReadPosts() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
postsList.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
Posts post = snapshot1.getValue(Posts.class);
for (String id : followingList) {
if (post.getPublisher().equals(id)) {
postsList.add(post);
}
}
}
postsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
这是模型代码。
public class Posts {
private Bitmap postImage;
private String postText;
private String publisher;
private String postId;
public Posts(Bitmap postImage, String postText, String publisher, String postId) {
this.postImage = postImage;
this.postText = postText;
this.publisher = publisher;
this.postId = postId;
}
public Posts() {
}
public String getPostId() {
return postId;
}
public void setPostId(String postId) {
this.postId = postId;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Bitmap getPostImage() {
return postImage;
}
public void setPostImage(Bitmap postImage) {
this.postImage = postImage;
}
public String getPostText() {
return postText;
}
public void setPostText(String postText) {
this.postText = postText;
}
}
这次请帮帮我。在论坛上none帮助是没有意义的..
问题是来自数据库的快照返回 postImage 带有一个字符串(可能是图像的 link ) 并且在您的模型 class 中,postImage 是使用位图声明的。因此,String 不能直接转换为 Bitmap。所以,你必须改变模型 class 如:
从 private Bitmap postImage;
到 private String postImage;
然后你必须从 link 加载图像(可能正在使用 Glide):
如果 link 来自 FirebaseStorage,您可以像这样加载图像:
StorageReference ref = storageReference.child(posts.postImage).getDownloadUrl();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(ref)
.into(imageView);
如果link直接来自link图片,你可以这样做:
Glide.with(this /* context */)
.load(posts.postImage)
.into(imageView);
注意:您将需要 gradle Glide 的依赖项:
implementation 'com.github.bumptech.glide:glide:4.12.0'
嗯,因为 none 回答了我,所以我再次询问希望有一些有礼貌的人...我正在尝试检索上传到 Firebase 的图像。在数据快照中,它给出了以下错误:Can't convert object of type java.lang.String to type android.graphics.Bitmap
。我尝试了一些方法来解决这个问题,但没有任何改变。这是代码:
这是读取 post 并将其显示在屏幕上的代码。
private void ReadPosts() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
postsList.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
Posts post = snapshot1.getValue(Posts.class);
for (String id : followingList) {
if (post.getPublisher().equals(id)) {
postsList.add(post);
}
}
}
postsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
这是模型代码。
public class Posts {
private Bitmap postImage;
private String postText;
private String publisher;
private String postId;
public Posts(Bitmap postImage, String postText, String publisher, String postId) {
this.postImage = postImage;
this.postText = postText;
this.publisher = publisher;
this.postId = postId;
}
public Posts() {
}
public String getPostId() {
return postId;
}
public void setPostId(String postId) {
this.postId = postId;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Bitmap getPostImage() {
return postImage;
}
public void setPostImage(Bitmap postImage) {
this.postImage = postImage;
}
public String getPostText() {
return postText;
}
public void setPostText(String postText) {
this.postText = postText;
}
}
这次请帮帮我。在论坛上none帮助是没有意义的..
问题是来自数据库的快照返回 postImage 带有一个字符串(可能是图像的 link ) 并且在您的模型 class 中,postImage 是使用位图声明的。因此,String 不能直接转换为 Bitmap。所以,你必须改变模型 class 如:
从 private Bitmap postImage;
到 private String postImage;
然后你必须从 link 加载图像(可能正在使用 Glide):
如果 link 来自 FirebaseStorage,您可以像这样加载图像:
StorageReference ref = storageReference.child(posts.postImage).getDownloadUrl();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(ref)
.into(imageView);
如果link直接来自link图片,你可以这样做:
Glide.with(this /* context */)
.load(posts.postImage)
.into(imageView);
注意:您将需要 gradle Glide 的依赖项:
implementation 'com.github.bumptech.glide:glide:4.12.0'