我正在尝试将备份和恢复应用数据库的功能合并到google drive app文件夹(对用户不可见)。我浏览了为android提供的基本设置指南setup guide,可以让用户授权应用程序并到达调用onConnected方法的位置。
我面临的问题是,我不知道如何将数据库文件(.db)从设备发送到google drive app文件夹。 Google已经共享了用于创建新文件的代码段,但就此而言。我确实发现了一个先前问过的问题,与我正在寻找的Google drive to back up and restore database and shared preferences of Android application模糊地相似,但后来又找不到我要找的东西。
搜索谷歌不会返回任何有用的链接,因为这是相对较新的API。
更新1:
共享onConnected()代码,
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)
);
}
更新2:
下面分享的解决方案非常适合将数据库上传到google drive app文件夹。对于可能遇到类似问题的用户,请尝试将数据库路径更改为"/data/data/com.abc.efg/databases/" + db_name
而不是"/data/com.abc.efg/databases/" + db_name
下一步是能够从google drive app文件夹中检索和恢复数据库。如果我能够让它工作,请更新这个问题。
答案 0 :(得分:12)
假设您有 MyDbFile.db 文件的路径,您可以使用这样的结构:
...
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's 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's 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(); }
}
祝你好运