上传文件到firebase存储失败,java安全异常,权限被拒绝



我想写一个简单的函数来注册一个用户到firebase并上传他的个人资料图片到firebase存储

我当前拥有的代码:

public static void createNewUser(String email, String username, String password, @Nullable Uri profileImage) {
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(authTask -> {
if (authTask.isSuccessful()) {
assert auth.getCurrentUser() != null;
String uid = auth.getCurrentUser().getUid();
StorageReference storageReference = storage.getReference("profileImages/" + uid);
if (profileImage != null)
storageReference.putFile(profileImage);
}
else throw new RuntimeException("Error creating user!");
});
}

Uri用以下代码检索:(Uri是设备存储中图像的路径)

ActivityResultLauncher<String> contentGetter = registerForActivityResult(new ActivityResultContracts.GetContent(), uri -> {
profileImage.setImageURI(uri);
this.profileImageUri = uri;
});
contentGetter.launch("image/*");

注意:错误是从"storageReference.putFile(profileImage);"抛出的

当我运行这段代码时,我得到以下错误:

. lang。从ProcessRecord{fcb00f2 10396:com打开provider com.android.providers.media. mediadocumentsprovider。mediamania/u0a146} (pid=10396, uid=10146)要求您使用ACTION_OPEN_DOCUMENT或相关api获得访问权限

我目前的前提:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

你知道这是什么原因吗?

找到了错误,我在文件选择器中使用了错误的意图,下面是代码片段:

ActivityResultLauncher<Intent> chooserLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() != Activity.RESULT_OK)
throw new RuntimeException("Error choosing image!");
Intent data = result.getData();
// Process result
}
);
// Configure chooser
Intent chooser = new Intent(Intent.ACTION_OPEN_DOCUMENT);
chooser.addCategory(Intent.CATEGORY_OPENABLE);
chooser.setType("image/*");

相关内容

  • 没有找到相关文章

最新更新