【问题标题】:Security Exception when calling user.getPhotoUrl() Firebase调用 user.getPhotoUrl() Firebase 时出现安全异常
【发布时间】:2019-12-10 01:12:44
【问题描述】:

我目前正在开发一个 android 应用程序,我必须允许用户注册个人资料图片,我可以使用用户名、电子邮件和照片等字段注册用户。当我尝试使用刚刚创建的同一用户重新登录时出现问题,这给了我以下错误....

Permission Denial:从 ProcessRecord 打开提供程序 com.google.android.apps.docs.storagebackend.StorageBackendContentProvider ......................要求您使用 ACTION_OPEN_DOCUMENT 或相关 API 获得访问权限

下面是我创建用户的代码

mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {

                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

                        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                .setDisplayName(uname).setPhotoUri(ImgUri).build();

                        user.updateProfile(profileUpdates);
                        Toast.makeText(getApplicationContext(), "Registration successful!", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                        Intent intent = new Intent(RegisterActivity.this, Login.class);
                        startActivity(intent);
                        finish();
                    }
                    else {
                        Toast.makeText(getApplicationContext(), "Registration failed! Please try again later", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                    }
                }
            });

这是我挑选图片的代码

private void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        ImgUri = data.getData();
        System.out.println(ImgUri);

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), ImgUri);
            // Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = findViewById(R.id.userImg);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

下面是我尝试检索用户个人资料图片时的代码

  FirebaseUser user = mAuth.getCurrentUser();
    System.out.println(user.getPhotoUrl());
    img.setImageURI(user.getPhotoUrl());

我需要将 ImageUri 保存到 Firebase 存储以及 Firebase 实时数据库吗?或者是否需要任何权限?我需要吗

【问题讨论】:

  • 只需使用intent.setAction(Intent.ACTION_OPEN_DOCUMENT); 而不是ACTION_GET_CONTENT
  • @diginoise 感谢您的帮助,我被困了好几个小时

标签: java android firebase firebase-authentication


【解决方案1】:

在拾取图像时向意图添加操作

private void chooseImage() {
   Intent intent=Intent(Intent.ACTION_OPEN_DOCUMENT);
   intent.setType("image/*");
   startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
   PICK_IMAGE_REQUEST);
}

ACTION_OPEN_DOCUMENT 不能替代 ACTION_GET_CONTENT。您应该使用哪一种取决于您的应用的需求:

如果您希望您的应用仅读取/导入数据,请使用 ACTION_GET_CONTENT。使用这种方法,应用会导入数据的副本,例如图像文件。

如果您希望您的应用能够长期、持久地访问文档提供者拥有的文档,请使用 ACTION_OPEN_DOCUMENT。一个例子是照片编辑应用程序,它允许用户编辑存储在文档提供程序中的图像。

【讨论】:

    【解决方案2】:

    设置 Firebase 存储规则以访问存储。

    打开 firebase deshboard >> go on storage >> in storage 在规则选项卡上移动。 更改您自己的存储访问规则,我正在与所有访问权限共享规则。

    service firebase.storage {
      match /b/shoppingappwithfcm.appspot.com/o {
        match /{allPaths=**} {
          allow read, write;
        }
      }
    } 
    

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2019-03-14
      • 1970-01-01
      相关资源
      最近更新 更多