1

I am trying to use the setPhotoUri for my users at the moment of creating the their profile, I am using two seperate methods first to upload and then get the url, both are called from within the "createUserWithEmailAndPassword" method but I am getting this error message: com.google.firebase.storage.StorageException: Object does not exist at location.

I have found some information Here, Here and Here, have tried to implement it on my code but nothing works.

This is the method used to create the user:

 auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            } else {

                               user = auth.getCurrentUser();
                                if(user!=null){
                                    uploadImage();
                                    getImageUrl();

                                    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                           .setPhotoUri(downloadUri)
                                            .setDisplayName(displayNameString).build();
                                    user.updateProfile(profileUpdates);
                                    FirebaseAuth.getInstance().signOut();
                                    Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
                                    intent.putExtra("Email",user.getEmail());

                                    if (user.getPhotoUrl()!=null){
                                        intent.putExtra("Image",user.getPhotoUrl().toString());
                                    }

                                    startActivity(intent);
                                    Toast.makeText(SignupActivity.this, "Verifique su contraseƱa",
                                            Toast.LENGTH_SHORT).show();
                                }


                            }
                        }
                    });

these are the methods to upload and get the url:

 private void uploadImage() {

    if(filePath != null)
    {
        ref = storageReference.child("UserProfileImages/"+user.getEmail() );
        ref.putFile(filePath); //I checked on my project and the file is successfully uploaded.
    }
}

// If I use ref.toString() the result perfectly matches the path of the file on my project

public void getImageUrl (){

    ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {

             downloadUri = uri;// The string(file link) that I need to use the setPhotoUri for my user.
            Log.i("Class","Photo "+ downloadUri);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
            Log.i("Class","an error occurred "+ exception.toString());
        }
    });
    }
mehul chauhan
  • 1,792
  • 11
  • 26
Fran Tardencilla
  • 289
  • 1
  • 2
  • 13

1 Answers1

1

In order to not receive this error message the file needs to be uploaded first, wait sometime until the file is stored and then get the download Url.

I fixed this problem by adding an oncompletelistener to the ref.putFile() method

Fran Tardencilla
  • 289
  • 1
  • 2
  • 13