android


Create or Open native Google documents using GoogleDriveApi


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!");
}
}
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.
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){
})

Related Links

Exception while parsing the url
ANDROID - positioning views programatically
About Android SDK add-on development
jQuery UI Sortable and Dragable NOT working on iPad, iPhone or Android
Launch another activity if checkbox is checked
phonegap android edittext is hidden behind virtual keyboard
Scroll a View and refresh content
Clickable textview link
Can we have 2 Application running simultaneously on an android device?
Not Scrolling elements inside HTML pages in phonegap in Android version
Google maps zoom controls not displayed fully
Android: Background images for relative layout
MapView onTap issue: android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
Android: How do i set the source of imageview to a bitmap in a bitmap array
create dyanamic rule
use html5 for android design

Categories

HOME
hyperlink
prism
fuelux
css3
redux-form
saml-2.0
grafana
r-googlesheets
simulink
visual-studio-emulator
echo
joomla3.0
pega
line
jsonpath
naivebayes
selectize.js
capistrano3
powershell-v5.0
standards
symfony-console
production
connector
selinux
sar
drive
pimcore
flask-sqlalchemy
monit
interpreter
direct3d11
windows-server-2003
licensing
angularjs-components
rpgle
sinch
ceylon
quickfixj
code-snippets
joomla2.5
getpixel
html-encode
least-squares
latitude-longitude
reason
sql-server-ce
xcode7.3
xdocreport
odroid
metaprogramming
spring-mvc-test
kill
trigonometry
skylink
naming
file-sharing
oim
rfc3339
ovf
user-defined-fields
activeperl
payara-micro
node-request
redbeard-ios
segment-io
greatest-n-per-group
expectations
android-dateutils
vorpal.js
jparsec
mobiledoc-kit
django-urls
iced-coffeescript
rrule
xmltype
nodelist
administration
formview
gii
gnumeric
terminate
spring-3
sphinxql
breakout
beaker-testing
actiondispatch
server-administration
twisted.web
device-width
mkv
wcf-configuration
rmiregistry
adobe-media-server
jsr286
cdonts
codebase
updatesourcetrigger
opml
sifr
duplex
wordprocessingml
wysiwym

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App