简体   繁体   中英

Not retrieving Profile Image from firebase database

"I am trying to upload a profile photo into firebase. but it's not retrieving into the application. when I upload a photo it's working. but not retrieving.how to fix this problem?."

'Here, when the user selects the image, here we can crop that image. and also here that image uploaded to the 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, "Error: " + message, Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    }
                });
            }
        }
    }

'In this part Retrieving user information and image file.'

 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) {

                    }
                });
    }

I am not familiar with Picasso but Glide. So I will just write a theory on top of it. Let's get it on.

The issue might be caused by Firebase Storage denying such request as there's probably no user authentication header attached to such request through Picasso. To verify this, temporarily enable public read access to your Firebase Storage.

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

If this is the case, your image will be displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM