Backing up / restoring SQLlite database to Google Drive application folder

I am trying to include functionality for backing up and restoring an application database in the Google applications folder for drives (not visible to the user). I reviewed the basic setup guide the setup guide given for android and can make the user authorize the application and go to the point where the onConnected method is called.

The problem I encountered is that I'm not sure how to go about β€œsending” the database file (.db) from the device to the Google Drive application folder. Google provided a common snippet for creating a new file, but more on that. I found a previously asked question, vaguely similar to what I'm looking for a Google drive to backup and restore the database and general settings of the Android application , but again not what I'm looking for.

A google search does not return useful links, perhaps because it is a relatively new api.

UPDATE 1:

Sharing 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 below works fine for loading the database into the Google Drive application folder. For those who may encounter a 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

The next step is to get and restore the database from the Google Drive application folder. Must update this question if I can get it to work.

+4
android android-sqlite google-drive-android-api
source share
1 answer

Assuming you have a path to the MyDbFile.db file, you can use this construct:

  ... saveToDrive( Drive.DriveApi.getAppFolder(getGoogleApiClient()), "MyDbFile.db", "application/x-sqlite3", new java.io.File("\...\...\...\MyDbFile.db") ); ... DriveId mDriveId; /****************************************************************** * create file in GOODrive * @param pFldr parent ID * @param titl file name * @param mime file mime type (application/x-sqlite3) * @param file file (with content) to create */ void saveToDrive(final DriveFolder pFldr, final String titl, final String mime, final java.io.File file) { if (getGoogleApiClient() != null && pFldr != null && titl != null && mime != null && file != null) try { // create content from file Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveContentsResult>() { @Override public void onResult(DriveContentsResult driveContentsResult) { DriveContents cont = driveContentsResult != null && driveContentsResult.getStatus().isSuccess() ? driveContentsResult.getDriveContents() : null; // write file to content, chunk by chunk if (cont != null) try { OutputStream oos = cont.getOutputStream(); if (oos != null) try { InputStream is = new FileInputStream(file); byte[] buf = new byte[4096]; int c; while ((c = is.read(buf, 0, buf.length)) > 0) { oos.write(buf, 0, c); oos.flush(); } } finally { oos.close();} // content COOL, create metadata MetadataChangeSet meta = new Builder().setTitle(titl).setMimeType(mime).build(); // now create file on GooDrive pFldr.createFile(getGoogleApiClient(), meta, cont).setResultCallback(new ResultCallback<DriveFileResult>() { @Override public void onResult(DriveFileResult driveFileResult) { if (driveFileResult != null && driveFileResult.getStatus().isSuccess()) { DriveFile dFil = driveFileResult != null && driveFileResult.getStatus().isSuccess() ? driveFileResult.getDriveFile() : null; if (dFil != null) { // BINGO , file uploaded dFil.getMetadata(getGoogleApiClient()).setResultCallback(new ResultCallback<MetadataResult>() { @Override public void onResult(MetadataResult metadataResult) { if (metadataResult != null && metadataResult.getStatus().isSuccess()) { DriveId mDriveId = metadataResult.getMetadata().getDriveId(); } } }); } } else { /* report error */ } } }); } catch (Exception e) { e.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } } /******************************************************************* * get file contents */ void readFromGooDrive() { byte[] buf = null; if (getGoogleApiClient() != null && getGoogleApiClient().isConnected()) try { DriveFile df = Drive.DriveApi.getFile(getGoogleApiClient(), mDriveId); df.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null) .setResultCallback(new ResultCallback<DriveContentsResult>() { @Override public void onResult(DriveContentsResult driveContentsResult) { if ((driveContentsResult != null) && driveContentsResult.getStatus().isSuccess()) { DriveContents cont = driveContentsResult.getDriveContents(); // DUMP cont.getInputStream() to your DB file cont.discard(getGoogleApiClient()); // or cont.commit(); they are equiv if READONLY } } }); } catch (Exception e) { e.printStackTrace(); } } 

Luck

+12
source share

All Articles