Backup/Restore a Realm database on Google Drive with Drive API (2/3)

Paolo Rotolo
Apr 22, 2016 · 3 min read
Image for post
Image for post
Image for post
Image for post
private void openFolderPicker() {
try {
if (mGoogleApiClient!=null && mGoogleApiClient.isConnected()){
if (intentPicker==null)
intentPicker = buildIntent();

// Start the intent to choose a folder
startIntentSenderForResult(
intentPicker, REQUEST_CODE_PICKER, null, 0, 0, 0);
}
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Unable to send intent", e);
showErrorDialog();
}
}
private IntentSender buildIntent(){
return Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[]{DriveFolder.MIME_TYPE})
.build(mGoogleApiClient);
}
Image for post
Image for post
private void openFilePicker() {
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
// these mimetypes enable these folders/files
// types to be selected
// In this case we only need to open plain text
.setMimeType(new String[] { DriveFolder.MIME_TYPE, "text/plain"})
.build(mGoogleApiClient);
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_SELECT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Unable to send intent", e);
showErrorDialog();
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_PICKER:
intentPicker = null;

if (resultCode == RESULT_OK) {
//Get the folder drive id
DriveId mFolderDriveId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

uploadToDrive(mFolderDriveId);
}
break;

case REQUEST_CODE_SELECT:
if (resultCode == RESULT_OK) {
// get the selected item's ID
DriveId driveId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

DriveFile file = driveId.asDriveFile();
downloadFromDrive(file);

} else {
showErrorDialog();
}
finish();
break;

}
}

Glucosio Project

A free and open source project, Glucosio aims to help…

Paolo Rotolo

Written by

Android developer @ Wideverse, Co-lead @GDG Bari. B.Sc. in Computer Engineering at #PoliBa. Big #OpenSource, #Kotlin fan. Making the world a better place

Glucosio Project

A free and open source project, Glucosio aims to help people with diabetes.

Paolo Rotolo

Written by

Android developer @ Wideverse, Co-lead @GDG Bari. B.Sc. in Computer Engineering at #PoliBa. Big #OpenSource, #Kotlin fan. Making the world a better place

Glucosio Project

A free and open source project, Glucosio aims to help people with diabetes.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store