无法从Firebase数据库检索配置文件映像

问题描述

“我正在尝试将个人资料照片上传到firebase中,但没有检索到应用程序中。当我上传照片时,它正在工作。但是没有检索到。如何解决此问题?”

'在这里,当用户选择图像时,我们可以在此处裁剪该图像。并在此将图片上传到Firebase。'

 @Override
    protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        if (requestCode == gallery && resultCode == RESULT_OK && data!= null){

            Uri imageUri = data.getData();

            // start picker to get image for cropping and then use the image in cropping activity
            CropImage.activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1,1)
                    .start(this);
        }
        //crop image
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if (resultCode == RESULT_OK){
                progressDialog.setTitle("Set profile Image");
                progressDialog.setMessage("Your profile image is updating..");
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();

                Uri resultUri = result.getUri();

                final StorageReference filePath = profileImageReference.child(currentUserID + ".jpg");//new profile image created

                //upload image to firebase storage
                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if (task.isSuccessful()){
                            Toast.makeText(SettingsActivity.this,"Profile Image uploaded...",Toast.LENGTH_SHORT).show();

                            final String downloadUri = filePath.getDownloadUrl().toString();

                            rootReference.child("Users").child(currentUserID).child("image")
                                    .setValue(downloadUri)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()){
                                                Toast.makeText(SettingsActivity.this,"Image Saved",Toast.LENGTH_SHORT).show();
                                            }
                                            else {
                                                String message = task.getException().toString();
                                                Toast.makeText(SettingsActivity.this,"Error: " + message,Toast.LENGTH_SHORT).show();
                                            }
                                            progressDialog.dismiss();
                                        }
                                    });
                        }
                        else {
                            String message = task.getException().toString();
                            Toast.makeText(SettingsActivity.this,Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    }
                });
            }
        }
    }

“在本部分中,检索用户信息和图像文件。”

 private void RetrieveUserinformation() {
        rootReference.child("Users").child(currentUserID)
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if ((snapshot.exists()) && (snapshot.hasChild("name") && (snapshot.hasChild("image")))){
                            String retrieveUserName = snapshot.child("name").getValue().toString();
                            String retrieveDescription = snapshot.child("description").getValue().toString();
                            String retrieveProfileImage = (String) snapshot.child("image").getValue();

                            userName.setText(retrieveUserName);
                            description.setText(retrieveDescription);
                            Picasso.get().load(retrieveProfileImage).into(profileImage);
                        }
                        else if ((snapshot.exists()) && (snapshot.hasChild("name"))){
                            String retrieveUserName = snapshot.child("name").getValue().toString();
                            String retrieveDescription = snapshot.child("description").getValue().toString();

                            userName.setText(retrieveUserName);
                            description.setText(retrieveDescription);

                        }
                        else{
                            Toast.makeText(SettingsActivity.this,"Please,Update your information...",Toast.LENGTH_SHORT).show();
                        }
                    }

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

                    }
                });
    }

解决方法

我对毕加索不熟悉,但对格莱德(Glide)很熟悉。因此,我将在此基础上写一个理论。让我们继续吧。

该问题可能是由Firebase Storage拒绝此类请求引起的,因为通过Picasso可能没有附加到该请求的用户身份验证标头。要验证这一点,请暂时启用对Firebase存储的公共读取访问权限。

rules_version = '2';
service firebase.storage {
    match /b/{bucket}/o {
        match /{allPaths=**} {
            allow write: if request.auth != null;
            allow read: if true;
        }
    }
}

在这种情况下,将显示您的图像。