Upload Sql Database To Google Drive Using Drive Api
I want to upload a database file to Google drive in my android app. I can back up the database file fine it saves to /sdcard/schoolbinder/backups/notes I used this website as a sta
Solution 1:
The demo you cited already has the sample for choosing a file.
/**
* Create a new file and save it to Drive.
*/privatevoidsaveFileToDrive() {
// Start by creating a new contents, and setting a callback.
Log.i(TAG, "Creating new contents.");
finalBitmapimage= mBitmapToSave;
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(newResultCallback<DriveContentsResult>() {
@OverridepublicvoidonResult(DriveContentsResult result) {
// If the operation was not successful, we cannot do anything// and must// fail.if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new contents.");
return;
}
// Otherwise, we can write our data to the new contents.
Log.i(TAG, "New contents created.");
// Get an output stream for the contents.OutputStreamoutputStream= result.getDriveContents().getOutputStream();
// Write the bitmap data from it.ByteArrayOutputStreambitmapStream=newByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
// Create the initial metadata - MIME type and title.// Note that the user will be able to change the title later.MetadataChangeSetmetadataChangeSet=newMetadataChangeSet.Builder()
.setMimeType("image/jpeg").setTitle("Android Photo.png").build();
// Create an intent for the file chooser, and start it.IntentSenderintentSender= Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
} catch (SendIntentException e) {
Log.i(TAG, "Failed to launch file chooser.");
}
}
});
}
Its just a matter of calling it. It seems that the demo is calling the Camera app and returning the Image
. Try to remove the if
condition in the onConnected
method and the saveFileToDrive
should be called and show a File picker.
Post a Comment for "Upload Sql Database To Google Drive Using Drive Api"