Reputation: 1960
To edit a Google Drive native doc on my Android app, I'm downloading the file to a .docx format (converting), editing the file in an editor capable of editing MS Word docx files, then I'm uploading the file back to its native Google doc format. I'm using the following code:
File liveFileInfo = sService.files().get(mItem.getFileId()).execute();
mInputStream = new FileInputStream(pFile); //pFile is the java.io.File of the source XXX.docx file
InputStreamContent mediaContent = new InputStreamContent("application/vnd.openxmlformats-officedocument.wordprocessingml.document",
new BufferedInputStream(mInputStream));
mediaContent.setLength(pFile.length());
Drive.Files.Update update = sService.files().update(mItem.getFileId(), liveFileInfo, mediaContent);
update.setConvert(true);
MediaHttpUploader uploader = update.getMediaHttpUploader();
int chunkSize = getChunkSize(mItem);
uploader.setChunkSize(chunkSize);
uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
@Override
public void progressChanged(MediaHttpUploader uploader)
throws IOException {
// update UI
}
});
File uploadedFile = update.execute();
The code runs without errors, but the contents of the file contains all the weird characters you normally see when the wrong format is uploaded or an incorrect file type.
So my question is whether it is possible to update an existing file using this process? Or is the conversion process only supported with INSERTed files? Is there anything wrong with the code above?
Upvotes: 1
Views: 288
Reputation: 1960
I figured out what I missed. Add the following line to the code above to fix the problem:
liveFileInfo.setMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Upvotes: 1