Uri创建文件时失败

时间:2019-04-06 14:36:09

标签: java android file android-file

我试图通过意图获取Exel文件管理器,但是当我从意图数据获取Uri并将其转换为文件对象时,文件返回不存在

  

这是我调用意图的代码

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/vnd.openxmlformats- 
officedocument.spreadsheetml.sheet");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
  

然后onActivityResult处理数据将其返回

if (resultCode == RESULT_OK && requestCode == REQUEST_CODE && data != null) {

     List<String> list;
     list = read(data.getData());

     for (int i = 0; i < list.size(); i++)
       Log.e("exel", list.get(i));
}

super.onActivityResult(requestCode, resultCode, data);
  

这里是我的#read方法

public List<String> read(@NonNull Uri inputFile) {

    List<String> resultSet = new ArrayList<>();
    Log.e("exel", FileChooser.getPath(this,inputFile));

    File sdDir = Environment.getExternalStorageDirectory();
    File inputWorkbook = new File(inputFile.getPath());

    if (inputWorkbook.exists()) {
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over column and lines
            for (int j = 0; j < sheet.getRows(); j++) {
                Cell cell = sheet.getCell(0, j);
                // if(cell.getContents().equalsIgnoreCase(key)){
                for (int i = 0; i < sheet.getColumns(); i++) {
                    Cell cel = sheet.getCell(i, j);
                    resultSet.add(cel.getContents());
                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        resultSet.add("File not found..!");
    }

    if (resultSet.size() == 0) {
        resultSet.add("Data not found..!");
    }
    return resultSet;
}

因此inputWorkbook.exists()方法始终返回false。

我真的在搜索此问题,因此请匹配并尝试另一种方法来查找真实路径,例如here

0 个答案:

没有答案