在我的Android应用程序中,我使用谷歌驱动器来选择文件,成功我可以获取内容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)
请帮我摆脱这个问题
答案 0 :(得分:1)
以下代码帮助我从内容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();
答案 1 :(得分:0)
答案 2 :(得分:0)
@OrkunKoçyiğit在这个SO线程中提供了一种更简单的方法(不使用GDrive API)