Android:无法获取文件路径onActivityResult

问题描述 投票:1回答:1

意图选择文件:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("*/*");
            startActivityForResult(intent, FILE_REQUEST_CODE);

onActivityResult代码:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FILE_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
          Uri uri = data.getData();
                assert uri != null;
                String uriString = uri.toString();
                File myFile = new File(uri.getPath());
                String path = getPathFromUri(uri);         

    }
   }

}

获取路径的代码是

private String getPathFromUri(Uri uri) {
        String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };
        Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String Path = cursor.getString(columnIndex);
        cursor.close();
        return Path;
    }

路径返回为null,我已经尝试了很多东西file.getPath()也不起作用...我错过了什么

android onactivityresult
1个回答
-1
投票

你有AndroidManifest的读写外部存储权限吗?

© www.soinside.com 2019 - 2024. All rights reserved.