How to export Google Docs as plain text using Android Drive API

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?

hey! yeah, google docs can be tricky since they’re not plain text. try the export option through the rest api, or check out google drive api v3 - it’s got better support. good luck!

Had this exact problem 6 months ago building a document reader app. The Android Drive API just can’t export Google Docs - it’s a known limitation Google never fixed before killing the API. I solved it with a hybrid approach: kept the Android API for regular files, but added REST calls specifically for Google Docs export. Check the MIME type (application/vnd.google-apps.document) to detect Google Docs, then hit the Drive REST API’s export endpoint with a separate HTTP request. You’ll need extra auth setup, but it gets you the plain text you’re after. Not too painful since you can reuse your existing OAuth credentials.

The Android Drive API doesn’t support exporting Google Docs files. Google Docs use a proprietary format, not plain text, so your code won’t work on them. You’ll need the REST API with the export parameter to convert them to text. I hit this same problem last year and had to switch to HTTP requests with Drive REST API v3. The export endpoint lets you specify text/plain as the MIME type. It’s more work, but there’s no other way to get readable content from Google Docs.