0
votes

I have been able to set up a Google Drive file picker so that I can read the contents of any file selected by the user. However, I would like to be able to download Google Docs files selected by the user as text files and then read their content, though I have only been able to find solutions to do this using the Drive Rest API, which I believe is separate from the Google Drive API for Android. Is there any solution that would allow me to do this? Currently, my code for downloading files is as follows:

final private class RetrieveDriveFileContentsAsyncTask
        extends ApiClientAsyncTask<DriveId, Boolean, String> {

    public RetrieveDriveFileContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected String doInBackgroundConnected(DriveId... params) throws IOException {
        String contents = null;
        DriveId driveId=params[0];
        DriveFile file = driveId.asDriveFile();
        DriveApi.DriveContentsResult driveContentsResult =
                file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
        if (!driveContentsResult.getStatus().isSuccess()) {
            return null;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();
        BufferedReader reader = new BufferedReader(new InputStreamReader(driveContents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
            builder.append('\n');
        }
        contents = builder.toString();
        driveContents.discard(getGoogleApiClient());
        return contents;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result == null) {
            return;
        }
        Intent intent=new Intent(getContext(),NotesActivity.class);
        intent.putExtra("setTextTo",result);
        startActivity(intent);
    }
}
1
The Android API is a wrapper around the REST API, so you can get some File object? And can you open a document and read the content to a String?OneCricketeer
I can read the content to a string of any file but files created in Google docs. Also, I don't think I can get the same file object.Daniel
Okay, if you can read the content, then you can write that to a text file, so what exactly is the problem?OneCricketeer
@cricket_007 I can't read from Google Docs files- only from text files or other files uploaded to Google Drive by the user.Daniel
Is there an error that occurs when you use a DriveId for a Google Doc document?OneCricketeer

1 Answers

2
votes

No Google Drive Android API doesn't provide the convert functionality. You would need to use the REST API for that.