Domanda

I have been closely following the documentation for the Google Drive Android API and, all works great. I can create new text documents and read them back in using the mime type of text/plain.

What I cannot do is create a native Google "Document" or "Spreadsheet." Actually, I can create them by using the mime type to application/vnd.google-apps.document or application/vnd.google-apps.spreadsheet as per Supported MIME Types documentation.

If, however, I try to write content to these documents, the documents never get uploaded. If I try to read documents that have content (content I created via a web browser) my openContents call fails.

Again, I can create text/plain documents and write to them, but they are not native Google Documents. I have scowered the documentation and sample files, but nothing describes what I'm looking for.

This seems so basic. Does the new GoogleApiClient not support doing this? What am I missing or doing wrong?

Here is the core code for creating. I have a similar issue when trying to read a application/vnd.google-apps.document but I'm sure the two issues are related. I'll spare the verbosity of "read" code.

private void exportToGDriveFile() {
    Drive.DriveApi.newContents(getGoogleApiClient()).setResultCallback(createNewFileCallback);
}



final private ResultCallback<ContentsResult> createNewFileCallback = new ResultCallback<ContentsResult>() {

    @Override
    public void onResult(ContentsResult result) {

        if (!result.getStatus().isSuccess()) {
            writeLog("Error while trying to create new file contents");
            return;
        }

        String fileName = getIncrementedFileName();
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle(fileName)
                .setMimeType("text/plain") // <-- This works! I can write and read back :) 
                //.setMimeType("application/vnd.google-apps.document") <-- can create if no contents are included.
                //.setMimeType("application/vnd.google-apps.spreadsheet") 
                .setStarred(true)
                .build();

        writeLog("creating file: " + fileName);

        // create a file on root folder
        Drive.DriveApi.getRootFolder(getGoogleApiClient())
                .createFile(getGoogleApiClient(), changeSet, result.getContents())
                .setResultCallback(afterCreateFileCallback);
    }
};



private ResultCallback<DriveFileResult> afterCreateFileCallback = new ResultCallback<DriveFileResult>() {

    @Override
    public void onResult(DriveFileResult result) {

        if (!result.getStatus().isSuccess()) {
            writeLog("Error while trying to create the file");
            return;
        }

        DriveFile driveFile = result.getDriveFile();
        writeLog("Created file " + driveFile.getDriveId());
        new WriteFileAsyncTask().execute(driveFile);

    }
};

private class WriteFileAsyncTask extends AsyncTask<DriveFile, Void, Boolean> {

    @Override
    protected Boolean doInBackground(DriveFile... args) {

        DriveFile file = args[0];
        try {


            ContentsResult contentsResult = file.openContents(getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
            if (!contentsResult.getStatus().isSuccess()) {
                return false;
            }


/************************
If I try to write content here, `application/vnd.google-apps.document` files will not upload.
*************************/

            String contents = "Hello World";

            OutputStream outputStream = contentsResult.getContents().getOutputStream();
            outputStream.write(contents.getBytes());
            com.google.android.gms.common.api.Status status = file.commitAndCloseContents(
                    getGoogleApiClient(), contentsResult.getContents()).await();
            return status.getStatus().isSuccess();
        } catch (IOException e) {
            // toast("IOException while appending to the output stream");
        }

        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        if (!result) {
            // toast("Error while editing contents");
            return;
        }
        // toast("Successfully uploaded Quizifications!");
    }

}
È stato utile?

Soluzione

It's not currently possible to read or edit the contents of Google Documents, Spreadsheets or Presentation files. They files are of a special type that don't have standard binary content, so you can't read and write from them in the same way you can from other files.

You can, however, interact with the metadata of existing files.

Sorry for the confusion, we should update the behavior so that its clear that its not possible.

Altri suggerimenti

Updating Google Docs with HTML is simple. Just make an api request with html-formatted text in the body (html tag is required) and content-type to be google docs, then your created/updated file will be available to the user as a Google Doc with all the formatting options.

           request({
             uri: 'https://www.googleapis.com/upload/drive/v2/files/'+fileId,
             method: 'PUT',
             qs: {
               uploadType: 'media'
             },
             form: '<html> Hello <b>World!</b> </html>',
             headers: {
               'Content-Type': 'application/vnd.google-apps.document',
               'Authorization': 'Bearer ' + access_token
             }},  function (error, response, body){


           })
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top