I am trying to incorporate the functionality to backup and restore the app database to google drives app folder (NOT visible to the user). I went through the basic setup guide setup guide given for android and can get the user to authorize the app and reach to the point where onConnected method is being called.
The problem that I am facing is, I am not sure how to go about 'sending' the database file (.db) from device to google drive app folder. Google has shared snippet for creating a new file but that's about it. I did find a previously asked question vaguely similar to what I am looking for Google drive to back up and restore database and shared preferences of Android application but then again not what I am looking for.
Searching google doesn't return any helpful links possibly because this is relatively newer api.
UPDATE 1:
Sharing the onConnected() code,
public void onConnected(Bundle bundle) {
Toast.makeText(GoogleSignIn.this, "In onConnected activity", Toast.LENGTH_SHORT).show();
// Testing to see if database is indeed uploaded to google drive app folder
String db_name = "XXX_DB";
String currentDBPath = "/data/" + "com.abc.efg" + "/databases/" + db_name;
saveToDrive(
Drive.DriveApi.getAppFolder(mGoogleApiClientDrive),
"XXX_DB.db",
"application/x-sqlite3",
new java.io.File(currentDBPath)
);
}
UPDATE 2:
The solution shared below works perfectly for uploading database to google drive app folder. For those who might face similar problem try changing the database path to "/data/data/com.abc.efg/databases/" + db_name
instead of "/data/com.abc.efg/databases/" + db_name
Next step is to be able to retrieve and restore database from google drive app folder. Shall update this question if I am able to get it working.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…