如何使用Firebase中的片段检索个人资料照片?

时间:2016-12-15 21:47:56

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

我正在尝试从Firebase检索个人资料图片,而我正在使用片段。

我是碎片的新手。这是我的Firebase数据库结构:

database

存储

Storage

这是我的片段:

public class Profile extends Fragment {

    private CircleImageView mprofilePic;
    private TextView mUsername, mDescription, mEditProfile;
    private ImageButton mSettings;

    public Profile() {
        // Required empty public constructor
    }

    public static Profile newInstance(int instance) {
        Bundle args = new Bundle();
        args.putInt("argsInstance", instance);
        Profile profile = new Profile();
        profile.setArguments(args);
        return profile;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View profile = inflater.inflate(R.layout.fragment_profile, container, false);

        mprofilePic = (CircleImageView) profile.findViewById(R.id.circleImageView_profileimage_profile);
        mUsername = (TextView) profile.findViewById(R.id.textView_username_profile);
        mDescription = (TextView)profile.findViewById(R.id.textView_info_profile);
        mEditProfile = (TextView)profile.findViewById(R.id.textView_editprofile_profile);
        mSettings = (ImageButton)profile.findViewById(R.id.imageButton_settings_profile);

        return profile;
    }        
}

1 个答案:

答案 0 :(得分:0)

Firebase.setAndroidContext(getActivity());
      Firebase ref = new Firebase(FIREBASE_URL);
       ref.child("users/0THnm4Tt6WNLm62D2lbtNlzTg2t2/image")
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.getValue() != null) {
                                String profilePic = "";
                                profilePic = (String) dataSnapshot.getValue();
                                // show image in imageview code
                            }
                        }

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