I managed to create a file picker for Google Drive that lets users select and read regular files. But I’m stuck when it comes to Google Docs files. I want to export them as plain text so I can read their content in my app.
Most solutions I found online use the REST API instead of the Android Drive API. I’m wondering if there’s a way to do this with the regular Android API that I’m already using.
Here’s my current code for reading file contents:
private class FetchFileContentTask extends ApiClientAsyncTask<DriveId, Boolean, String> {
public FetchFileContentTask(Context ctx) {
super(ctx);
}
@Override
protected String doInBackgroundConnected(DriveId... fileIds) throws IOException {
String fileContent = null;
DriveId selectedFile = fileIds[0];
DriveFile driveFile = selectedFile.asDriveFile();
DriveApi.DriveContentsResult contentResult =
driveFile.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
if (!contentResult.getStatus().isSuccess()) {
return null;
}
DriveContents contents = contentResult.getDriveContents();
BufferedReader fileReader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
StringBuilder textBuilder = new StringBuilder();
String currentLine;
while ((currentLine = fileReader.readLine()) != null) {
textBuilder.append(currentLine);
textBuilder.append('\n');
}
fileContent = textBuilder.toString();
contents.discard(getGoogleApiClient());
return fileContent;
}
@Override
protected void onPostExecute(String content) {
super.onPostExecute(content);
if (content == null) {
return;
}
Intent newIntent = new Intent(getContext(), TextEditorActivity.class);
newIntent.putExtra("documentText", content);
startActivity(newIntent);
}
}
This works fine for regular files but doesn’t handle Google Docs properly. Any ideas?