如何在gridview中显示Firebase照片?

时间:2017-03-08 19:44:15

标签: android gridview firebase firebase-realtime-database firebase-storage

我有Firebase用户和他们所做的帖子。我想从帖子中获取帖子图片网址,下载并在网格视图中显示它们。

这是我的代码:

public class Profile extends Fragment {


  private CircleImageView mprofilePic;
  private TextView mUsername, mBio;
  private GridView mGridView;

  private Uri mImageUri = null;
  private FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  private StorageReference mStorageImage;
  private DatabaseReference mdatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");


  private FirebaseAuth mAuth;
  private FirebaseUser mCurrentUser;
  private DatabaseReference userposts;

  private static final int GALLERY_REQUEST = 1;

  public static Profile newInstance() {
    return new Profile();
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View profileView = inflater.inflate(R.layout.fragment_profile, container, false);

    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();
    userposts = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid()).child("posts");


    mprofilePic = (CircleImageView) profileView.findViewById(R.id.profile_picture);
    mUsername = (TextView) profileView.findViewById(R.id.profile_username);
    mBio = (TextView) profileView.findViewById(R.id.profile_bio);

    mGridView = (GridView) profileView.findViewById(R.id.gridview);


    mdatabaseUsers.child(firebaseUser.getUid()).child("image")
      .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          String imageUrl = dataSnapshot.getValue().toString();
          Glide.with(getContext()).load(imageUrl).into(mprofilePic);
          //mprofilePic.setImageURI(Uri.parse(imageUrl));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
      });
    mdatabaseUsers.child(firebaseUser.getUid()).child("Name").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        String username = dataSnapshot.getValue().toString();
        mUsername.setText(username);
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
    });


    return profileView;
  }

}

this is the firebase database

我需要的是获取当前用户并获取子项(帖子)和子项(图像)然后下载并将其放入网格视图中,然后运行到下一篇文章并获取第二个帖子图像等等上。

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

<强> 1。声明这些

Firebase.setAndroidContext(this);
Firebase ref = new Firebase(YOUR_FIREBASE_URL);

<强> 2。您可以使用addChildEventListener

ref.child("Users/w3.....")
                .child("posts")
                .addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot snapshot, String s) {
                        Map<String, Object> value = (Map<String, Object>) snapshot.getValue();
                        String img = value.get(Constants.chnl_msg).toString(); // your image url
                    }

                    @Override
                    public void onChildChanged(DataSnapshot snapshot, String s) {

                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {
                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                    }
                });

第3。或addListenerForSingleValueEvent

ref.child("Users/w3.....")
                .child("posts").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Map<String, Object> value = (Map<String, Object>) dataSnapshot.getValue();
                String img = value.get(Constants.chnl_msg).toString(); // your image url
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });