如何将Firebase图像检索到ImageView?

huangapple go评论69阅读模式
英文:

How to retrieve Firebase images to ImageView?

问题

以下是我的Firebase实时数据库结构:

如何将Firebase图像检索到ImageView?

我想要检索每一个子项(images1、images2、images3、images4是子项名称)中的图片。

private void loadmybanners1() {
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("BannerImages");
    ref.orderByChild("images2").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {
                final String myImg1 = "" + ds.child("image").getValue();

                try {
                    Picasso.get().load(myImg1).networkPolicy(NetworkPolicy.OFFLINE).into(ban2Iv, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError(Exception e) {
                            Picasso.get().load(myImg1).into(ban2Iv);
                        }
                    });
                } catch (Exception e) {
                    ban2Iv.setImageResource(R.drawable.ic_image);
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

我已经使用这段代码来检索image2的图片。但是从中,我只能检索到images1子项的数据。需要进行哪些修改?

英文:

Here is my Firebase Realtime Database structure:

如何将Firebase图像检索到ImageView?

I want to retrieve each and every image in a child(images1,images2,images3,images4 are the child names)

private void loadmybanners1() {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("BannerImages");
        ref.orderByChild("images2").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot ds : snapshot.getChildren()) {
                    final String myImg1 = "" + ds.child("image").getValue();


                    try{
                        Picasso.get().load(myImg1).networkPolicy(NetworkPolicy.OFFLINE).into(ban2Iv, new Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError(Exception e) {
                                Picasso.get().load(myImg1).into(ban2Iv);
                            }
                        });
                    } catch (Exception e) {
                        ban2Iv.setImageResource(R.drawable.ic_image);
                    }
                }


            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

I have used this code to retrieve the image2. But from that, I can only retrieve the images1 child data. What are the modifications to do?

答案1

得分: 1

要获取BannerImages节点中的每个子节点,无需使用查询,可以简单地使用DatabaseReference,如以下代码所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference bannerImagesRef = rootRef.child("BannerImages");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String myImg1 = dataSnapshot.child("images1").child("image").getValue(String.class);
        Picasso.get().load(myImg1);
        String myImg2 = dataSnapshot.child("images2").child("image").getValue(String.class);
        Picasso.get().load(myImg2);

        // 对于其他images3、images4和images5子节点也可以这样处理
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); // 不要忽略错误!
    }
};
bannerImagesRef.addListenerForSingleValueEvent(valueEventListener);
英文:

> I want to retrieve each and every image in a child

To get each and every child within your BannerImages node, there is no need to use a query, you can simply use a DatabaseReference, as in the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference bannerImagesRef = rootRef.child("BannerImages");
ValueEventListener valueEventListener = new ValueEventListener() {
	@Override
	public void onDataChange(DataSnapshot dataSnapshot) {
		String myImg1 = dataSnapshot.child("images1").child("image").getValue(String.class);
		Picasso.get().load(myImg1)
		String myImg2 = dataSnapshot.child("images2").child("image").getValue(String.class);
		Picasso.get().load(myImg2)

        //And so on for the other images3, images4, and images5 children
	}

	@Override
	public void onCancelled(@NonNull DatabaseError databaseError) {
		Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
	}
};
bannerImagesRef.addListenerForSingleValueEvent(valueEventListener);

答案2

得分: 0

将这个依赖项添加到你的代码中:

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

从Firebase获取URL后,只需使用以下代码行:

Glide.with(context).load(Firebase URL).into(imageView);
英文:

Add this dependency

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

After getting Url from Firebase.Just Use below line

Glide.with(context).load(Firbase Url).into(imageView); 

huangapple
  • 本文由 发表于 2020年8月10日 17:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定