Android文件选择器绝对路径问题

And*_*eaF 3 android absolute-path filechooser android-intent

我使用intent过滤器来获取用户使用文件选择器选择的文件的路径,遗憾的是我有问题获取绝对路径,

onActivityResult的路径始终使用各种额外数据启动,这些数据会导致我的应用程序出错

例如

/content/:/myabsolutepath
Run Code Online (Sandbox Code Playgroud)

要么

file:///myabsolutepath
Run Code Online (Sandbox Code Playgroud)

额外的属性取决于文件类型,手机上的文件管理器等.

我只需要获得表单中的绝对路径

/myabsolutepath
Run Code Online (Sandbox Code Playgroud)

这里有我的代码

private void openFile() {
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.setType("file/*");
        startActivityForResult(i, FILE_REQ_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent i) {
        //String with the path;
        path = i.getDataString();


        super.onActivityResult(requestCode, resultCode, i);

    }
Run Code Online (Sandbox Code Playgroud)

小智 7

尝试:

path = i.getData().getPath();
Run Code Online (Sandbox Code Playgroud)