[英]DatabaseException: can't convert object of java.Lang.String to type
[英]Can't convert object of type String to type Bitmap
好吧,因為沒有人回答我,所以我再次詢問是否有一些有禮貌的人......我正在嘗試檢索上傳到 Firebase 的圖像。 In datasnapshot it gives the following error: Can't convert object of type java.lang.String to type android.graphics.Bitmap
. 我嘗試了一些方法來解決這個問題,但沒有任何改變。 這是代碼:
這是閱讀帖子並將其顯示在屏幕上的代碼。
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) {
}
});
}
這是 model 代碼。
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;
}
}
這次請幫幫我。 在論壇上沒有任何幫助是沒有意義的..
問題是來自數據庫的快照返回帶有字符串(可能是圖像的鏈接)的postImage ,並且在您的 model class 中, postImage是用 Bitmap 聲明的。 因此,字符串不能直接轉換為 Bitmap。 所以,你必須改變 model class 像:
來自private Bitmap postImage;
到private String postImage;
然后你必須從鏈接加載圖像(可能使用 Glide):
如果鏈接來自 FirebaseStorage,您可以像這樣加載圖像:
StorageReference ref = storageReference.child(posts.postImage).getDownloadUrl();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(ref)
.into(imageView);
如果鏈接來自圖像的直接鏈接,您可以這樣做:
Glide.with(this /* context */)
.load(posts.postImage)
.into(imageView);
注意:您將需要 Glide 的 gradle 依賴項:
implementation 'com.github.bumptech.glide:glide:4.12.0'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.