Android- Select 来自 Google 驱动器的 CSV 文件并在 java 中解析

Android- Select CSV file from Google Drive and parse it in java

我正在尝试从 Google 驱动器加载 CSV 文件并在 java 中解析它。但是每当我 select 文件时,它都会显示异常:找不到文件

下面是我实现的代码:

选择者意图代码:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("text/csv");   //XML file only
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    try
    {
        startActivityForResult(Intent.createChooser(intent, "Select"), 100);
    } catch (android.content.ActivityNotFoundException ex)
    {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Drive Not Found", Toast.LENGTH_SHORT).show();
    }

onActivity 结果:

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode)
    {
        case 100:
            {
            if (resultCode == RESULT_OK)
            {
                //onImport(new File(data.getData().getPath()));
                String filePath = null;
                Uri uri = data.getData();

                File file = new File(uri.toString());
                if (file.exists()) {
                    //Do something
                    System.out.println("Exists");
                    proImportCSV(new File(data.getData().getPath()));
                }
                else
                {
                    System.out.println("Not Exists");
                }

            }
        }
    }
}

使用 CSVReader

解析 CSV
   private void proImportCSV(File from)
{
    try
    {
        // reading CSV and writing table
        CSVReader reader = null;
        try
        {
            reader = new CSVReader(new FileReader(from));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String[] nextLine;
        try {
            while ((nextLine = reader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                System.out.println(nextLine[0] + nextLine[1] + "etc...");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

请告诉我为什么我收到文件未找到错误。另外,我是否正确解析了 csv?如果不对,请详细告诉我正确的。

提前致谢!

我已经使用 bufferReader 对其进行了解析,一切都很完美!

        Uri uri = data.getData();    
            BufferedReader mBufferedReader = null;
            String line;

             try
                {
                    InputStream inputStream = getContentResolver().openInputStream(uri);
                    mBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    while ((line = mBufferedReader.readLine()) != null)
                    {
                        System.out.println("LLLLL: "+ line);
                    }
                    mBufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }