Android 存储访问框架返回 "raw" 路径

Android storage access framework returning "raw" path

尝试使用存储访问框架 select 设备中的文件(我不想解析的 html 文件),但它 returns 一个文件路径似乎不是内容 URI (/document/raw:/storage/emulated/0/Download/test.html),当我将它与内容解析器一起使用时出现错误。

获取文件的意图:

boolean alreadyHasReadPermissions = hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE);    
if (alreadyHasReadPermissions) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("text/html");
    try {
        activity.startActivityForResult(intent, fileChooserResultCode);
    }catch(ActivityNotFoundException e){
        Toast.makeText(activity, R.string.unable_to_open_file_picker, Toast.LENGTH_LONG).show();
    }
}

读取文件的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==fileChooserResultCode){
        if(resultCode == Activity.RESULT_OK && data.getData()!=null) {
            String contentUriString = data.getData().getPath();
            try {
                Uri contentUri = Uri.parse(contentUriString);
                InputStream inputStream = getContentResolver().openInputStream(contentUri); // <<< Errors with FileNotFoundException
                BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
                ...
            }catch (NullPointerException | IOException e){
                Toast.makeText(this, R.string.unable_to_open_file, Toast.LENGTH_LONG).show();
            }
        }
    }
}

删除:

String contentUriString = data.getData().getPath();

并删除:

Uri contentUri = Uri.parse(contentUriString);

并更改:

InputStream inputStream = getContentResolver().openInputStream(contentUri);

至:

InputStream inputStream = getContentResolver().openInputStream(data.getData());

IOW,不要在 Uri 上调用 getPath()