Daniel
Daniel

Reputation: 1729

Download Google Docs document as text file using Google Drive API for Android

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

Upvotes: 0

Views: 807

Answers (1)

Shailendra
Shailendra

Reputation: 213

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

Upvotes: 2

Related Questions