无法为用户/对象设置setPhotoUri在位置不存在

时间:2018-01-03 14:10:45

标签: java android firebase-storage

我正在尝试在创建他们的个人资料时为我的用户使用setPhotoUri,我首先使用两个单独的方法上传然后获取网址,这两个方法都来自" createUserWithEmailAndPassword&#34 ;方法,但我收到此错误消息:com.google.firebase.storage.StorageException:对象在位置不存在。

我找到了一些信息HereHereHere,试图在我的代码上实现它但没有任何效果。

这是用于创建用户的方法:

 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();
                                }


                            }
                        }
                    });

这些是上传和获取网址的方法:

 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.
    }
}

//如果我使用ref.toString(),结果与项目中文件的路径完全匹配

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());
        }
    });
    }

1 个答案:

答案 0 :(得分:0)

为了不收到此错误消息,首先需要上传文件,等待一段时间直到存储文件,然后获取下载URL。

我通过在ref.putFile()方法中添加oncompletelistener来解决这个问题