Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t将字符串类型的对象转换为位图类型_Java_Android_Xamarin.android - Fatal编程技术网

Java Can';t将字符串类型的对象转换为位图类型

Java Can';t将字符串类型的对象转换为位图类型,java,android,xamarin.android,Java,Android,Xamarin.android,好吧,因为没有人回答我,我再次问,有一些有礼貌的人。。。我正在尝试检索Firebase上上载的图像。在datasnapshot中,它给出了以下错误:无法将java.lang.String类型的对象转换为android.graphics.Bitmap类型。我尝试了一些方法来解决这个问题,但没有任何改变。代码如下: 这是阅读文章并在屏幕上显示的代码 private void ReadPosts() { DatabaseReference reference = FirebaseDat

好吧,因为没有人回答我,我再次问,有一些有礼貌的人。。。我正在尝试检索Firebase上上载的图像。在datasnapshot中,它给出了以下错误:
无法将java.lang.String类型的对象转换为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) {

            }
        });
    }
这是模型代码

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是用位图声明的。因此,字符串不能直接转换为位图。因此,您必须更改模型类,如下所示:

私有位图后期图像
私有字符串后期图像

然后,您必须从链接加载图像(可能使用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);
如果链接来自图像的直接链接,您可以这样做:

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的渐变依赖性:

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

你能分享你的数据库吗structure@lyncx什么意思?我的数据库在Firebase上。谢谢你的回答。它解决了错误,但是post没有出现在屏幕上。可能有很多原因,您能否确认是否调用了onDataChange(),并按预期检索了数据(post)?