如何获取和显示个人资料图片Android Firebase?

时间:2018-09-27 06:01:08

标签: java android xml firebase firebase-storage

如何使用uid检索和显示当前用户的个人资料图片?我已经使用uid将图像连接到用户uid,但不确定如何获取和显示。

这是我将其上传到Firebase存储的方式

mProfilePic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_INTENT);
        }
    });

我让用户从手机存储中单击一个图像视图,并使用当前登录的用户uid上载该图像。

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK && data != null && data.getData() != null){
        mainImageURI = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mainImageURI);
            mProfilePic.setImageBitmap(bitmap);
            final ProgressDialog proDialog = new ProgressDialog(HomeActivity.this);
            proDialog.setTitle("Uploading...");
            proDialog.show();

            String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            UploadTask uploadTask = FirebaseStorage.getInstance().getReference().child("newFolder").child(userUid).putFile(mainImageURI);
            uploadTask
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                        }
                    });

            final StorageReference mRef = mStorage.child("Photos").child(mainImageURI.getLastPathSegment());
            mRef.putFile(mainImageURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    proDialog.dismiss();
                    Toast.makeText(HomeActivity.this, "Uploaded", Toast.LENGTH_LONG).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    proDialog.dismiss();
                    Toast.makeText(HomeActivity.this, "Upload Failed "+e.getMessage(), Toast.LENGTH_LONG).show();
                }
            })
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

维护一个数据库,您可以在其中保存针对uid的图像URL。然后使用该网址从数据库中检索您的个人资料图片

答案 1 :(得分:0)

将图像网址存储在具有uid和所有其他属性的文档中,您可以使用Glide库检索图像 https://github.com/bumptech/glide