1

In my android application i have used google drive to pick the file, successfully i can get the content uri, but having trouble to read the file

below is my content URI

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

The way i have used to read the file is below

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();
        }

Always i got File Not Found Exception

My 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)

Please help me to get rid of this problem

Kara
  • 6,115
  • 16
  • 50
  • 57

3 Answers3

1

This below code is helped me to read the file from content 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();
0

https://developers.google.com/drive/android/files might help you to solve your issue.

D M Patel
  • 1
  • 1
0

@Orkun Koçyiğit provides a simpler way to do it in this SO thread (without using the GDrive API)

Community
  • 1
  • 1
r.pedrosa
  • 709
  • 5
  • 12