简体   繁体   English

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

[英]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. 我已经能够设置Google云端硬盘文件选择器,以便读取用户选择的任何文件的内容。 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. 但是,我只能下载用户选择的Google Docs文件作为文本文件,然后读取其内容,尽管我只能使用Drive Rest API找到解决方案,我认为它与适用于Android的Google Drive API。 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);
    }
}

No Google Drive Android API doesn't provide the convert functionality. 没有Google Drive Android API不提供转换功能。 You would need to use the REST API for that. 您将需要使用REST API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM