Search code examples
javaandroidxamarin.android

Can't convert object of type String to type Bitmap


Well, because none answered me I am asking again hopping that there is some polite people... I am trying to retrieve images that are uploaded on Firebase. In datasnapshot it gives the following error: Can't convert object of type java.lang.String to type android.graphics.Bitmap. I tried some ways to solve the issue but nothing changed. Here is the code:

This is the code to read the post and show it on the screen.

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) {

            }
        });
    }

This is the model code.

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

Please this time help me. It is meaningless to be on a forum and none helping..


Solution

  • The problem is that the snapshot from the database returning postImage with a String (probably the link of the image) and in your model class, postImage is declared with a Bitmap. Hence, the String cannot be converted to Bitmap directly. So, you have to change the model class like:

    from private Bitmap postImage; to private String postImage;

    Then you have to load the image from the link (may be using Glide):

    If the link is from FirebaseStorage you can load the image like this:

    StorageReference ref = storageReference.child(posts.postImage).getDownloadUrl();
    Glide.with(this /* context */)
            .using(new FirebaseImageLoader())
            .load(ref)
            .into(imageView);
    

    If the link is from direct link of image, you can do like this:

    Glide.with(this /* context */)
            .load(posts.postImage)
            .into(imageView);
    

    Note: you will need gradle dependency of Glide:

    implementation 'com.github.bumptech.glide:glide:4.12.0'