I want to use Google Drive as server to store app data. So, I have written all the data to a file and then upload that file to Drive.
Code:
try {
// outputStream.write(bitmapStream.toByteArray());
outputStream.write(text.getBytes());
}
catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("text/txt").setTitle("testfile.txt").build();
IntentSender intentSender = 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.");
}
Is it possible that whenever the device is connected to internet, the app data will get synchronized with data available on Google Drive? I read about Google Drive API, but was unable to understand:
1) how will the synchronization happen? 2) can we synchronize with the file stored in App folder of Google Drive? 3) do I need to write the file to Google Drive or I'll have to save the file to some container and Google will update itself when connected to internet(as in ios)?
Please guide me.
EDIT
Before creating a file in drive, I have done a check.
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.MIME_TYPE, "text/plain"))
.addFilter(Filters.eq(SearchableField.TITLE, "appdata.txt")).build();
Drive.DriveApi.query(getGoogleApiClient(), query).setResultCallback(
metadataCallback);
final private ResultCallback<MetadataBufferResult> metadataCallback = new ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
metadata = result.getMetadataBuffer();
for (int i = 0; i < metadata.getCount(); i++) {
DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
metadata.get(i).getDriveId());
file.trash(getGoogleApiClient());
}
Is it a wrong way to proceed?
newCreateFileActivityBuilder() will start an activity to let the user choose a location in their Drive to create the file. If you want to use the App folder you'll need to follow the instructions in https://developers.google.com/drive/android/appfolder