Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自Uri的Android getPath()不工作_Android_File_Path - Fatal编程技术网

来自Uri的Android getPath()不工作

来自Uri的Android getPath()不工作,android,file,path,Android,File,Path,在装有android 7.0的华为荣誉8上,uri.getPath()返回类似于/external/file/3344的内容,而不是文件的实际路径。此代码在许多设备上以及装有android 7.0和uri.getPath()的android emulator上也可以正常工作。getPath()返回/storage/emulated/0/PCalculator/main.js,但在荣誉8上不返回。我使用Intent在我的程序中选择一个文件,如下所示: private void fileBrowse

在装有android 7.0的华为荣誉8上,uri.getPath()返回类似于/external/file/3344的内容,而不是文件的实际路径。此代码在许多设备上以及装有android 7.0和uri.getPath()的android emulator上也可以正常工作。getPath()返回/storage/emulated/0/PCalculator/main.js,但在荣誉8上不返回。我使用Intent在我的程序中选择一个文件,如下所示:

private void fileBrowse(){
    Intent intent = new Intent();
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser
                (intent,"Open File"),RCODE_OPENFILE);
}
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);

    switch(requestCode){
        case RCODE_OPENFILE:
            if(resultCode == RESULT_OK &&
                    data != null && data.getData() != null){
                fileOpen(data.getData());
            }
            break;
    }

}
在onActivityResult中,我想读取以下文件:

private void fileBrowse(){
    Intent intent = new Intent();
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser
                (intent,"Open File"),RCODE_OPENFILE);
}
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);

    switch(requestCode){
        case RCODE_OPENFILE:
            if(resultCode == RESULT_OK &&
                    data != null && data.getData() != null){
                fileOpen(data.getData());
            }
            break;
    }

}
以及fileOpen函数:

private void fileOpen(Uri uri){

    File mFile = new File(uri.getPath());
    StringBuilder mText = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(mFile));
        String line;

        while ((line = br.readLine()) != null) {
            mText.append(line);
            mText.append('\n');
        }
        br.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    etCode.setText(mText);
    tvTitle.setText(mFile.getName());
}
您无法获取“文件的真实路径”,请改用
InputStream
阅读
ContentResolver
API文档了解如何获取oneRefer。如果您无法获取“文件的真实路径”,请改用
InputStream
阅读
ContentResolver
API文档了解如何获取oneRefer