如何读取从 Google Drive 中挑选的 csv 文件

How to read the csv file which is pick from GoogleDrive

在我的 android 应用程序中,我使用 google 驱动器来选择文件,我可以成功获取内容 uri,但无法读取文件

下面是我的内容 URI

URI = content://com.google.android.apps.docs.files/exposed_content/qAJe%2Bv7DxEtFhc1liRudRA%3D%3D%0A%3BPE4h2Nrj6pmZwlvaI5ZUguH%2FgG6RxwwptVeTWBME4TLn8eY5ejaqJ0EXiaMxbkY%2B%0A

下面是我用来读取文件的方式

Uri _uri = data.getData();
    File file = new File(_uri.getPath());
    ArrayList<String> records = new ArrayList<String>();

        BufferedReader mBufferedReader = null;
        String line;
        try {
            mBufferedReader = new BufferedReader(new FileReader(filepath));
            while ((line = mBufferedReader.readLine()) != null) {
                records.add(line);
            }
            mBufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

我总是遇到文件未找到异常

我的Logcat

03-18 10:13:26.947: W/System.err(24044): java.io.FileNotFoundException: /exposed_content/qAJe+v7DxEtFhc1liRudRA==
03-18 10:13:26.947: W/System.err(24044): ;PE4h2Nrj6pmZwlvaI5ZUguH/gG6RxwwptVeTWBME4TLn8eY5ejaqJ0EXiaMxbkY+
03-18 10:13:26.947: W/System.err(24044): : open failed: ENOENT (No such file or directory)
03-18 10:13:26.947: W/System.err(24044):    at libcore.io.IoBridge.open(IoBridge.java:416)
03-18 10:13:26.947: W/System.err(24044):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-18 10:13:26.947: W/System.err(24044):    at java.io.FileReader.<init>(FileReader.java:42)

请帮我解决这个问题

https://developers.google.com/drive/android/files 可能会帮助您解决问题。

下面的代码帮助我从内容 uri 中读取文件

BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    builder.append(line);
}
String contentsAsString = builder.toString();

@Orkun Koçyiğit 在此 SO 线程中提供了一种更简单的方法(不使用 GDrive API)