簡體   English   中英

使用Android版Google Drive API將Google文檔文檔下載為文本文件

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

我已經能夠設置Google雲端硬盤文件選擇器,以便讀取用戶選擇的任何文件的內容。 但是,我只能下載用戶選擇的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);
    }
}

沒有Google Drive Android API不提供轉換功能。 您將需要使用REST API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM