使用Google Drive API for Android将Google文档文档下载为文本文件

时间:2016-07-18 03:17:26

标签: android google-drive-android-api

我已经能够设置Google Drive文件选择器,以便我可以读取用户选择的任何文件的内容。但是,我希望能够将用户选择的Google Docs文件下载为文本文件,然后阅读其内容,但我只能使用Drive Rest API找到解决方案,我相信它与适用于Android的Google Drive API。有没有任何解决方案可以让我这样做?目前,我的下载文件代码如下:

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 个答案:

答案 0 :(得分:2)

没有Google Drive Android API无法提供转换功能。您需要使用REST API。