【问题标题】:Not able to setPhotoUri for users /Object does not exist at location无法为用户设置照片Uri /对象在该位置不存在
【发布时间】:2018-06-13 05:26:28
【问题描述】:

我正在尝试在创建他们的个人资料时为我的用户使用 setPhotoUri,我首先使用两种单独的方法上传然后获取 url,两者都是从“createUserWithEmailAndPassword”方法中调用的,但我是收到此错误消息:com.google.firebase.storage.StorageException:该位置不存在对象。

我找到了一些信息Here、Here 和Here,已尝试在我的代码上实现它,但没有任何效果。

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

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


                            }
                        }
                    });

这些是上传和获取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.
    }
}

// 如果我使用 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());
        }
    });
    }

【问题讨论】:

    标签: java android firebase-storage


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多