在Stack Overflow上使用Google Drive API for Android和一些答案,我已设法在我的应用中实施Google登录,并将存储在用户设备上的SQLite数据库备份到Google云端硬盘。
供参考,以下是我将数据库保存到Google云端硬盘的方法(这是在名为final class
的{{1}}中完成的:
DriveDbHandler
所以这是我的问题:
我可以将数据库保存到Google云端硬盘,但如何通过本地更改来更新Google云端硬盘版本?
例如,我可以从表A中删除3行,然后在本地向表B添加5行(到设备的SQLite数据库),但如何通过此更改更新Google Drive版本?
我考虑过删除整个云端硬盘文件并重新上传,但根据我的理解,这会导致该文件的private static final String LOG_TAG = "DriveDbHandler";
private static final String PACKAGE_NAME = "com.package.example";
private static final String DATABASE_PATH =
"/data/data/" + PACKAGE_NAME + "/databases/" + DbHelper.DATABASE_NAME;
private static final String FILE_NAME = DbHelper.DATABASE_NAME;
private static final String MIME_TYPE = "application/x-sqlite-3";
private DriveDbHandler() {
}
public static void tryCreatingDbOnDrive(final GoogleApiClient googleApiClient) {
// We need to check if the database already exists on Google Drive. If so, we won't create
// it again.
Query query = new Query.Builder()
.addFilter(Filters.and(
Filters.eq(SearchableField.TITLE, FILE_NAME),
Filters.eq(SearchableField.MIME_TYPE, MIME_TYPE)))
.build();
DriveFolder appFolder = Drive.DriveApi.getAppFolder(googleApiClient);
appFolder.queryChildren(googleApiClient, query).setResultCallback(
new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(@NonNull DriveApi.MetadataBufferResult metadataBufferResult) {
if (!metadataBufferResult.getStatus().isSuccess()) {
Log.e(LOG_TAG, "Query for " + FILE_NAME + " unsuccessful!");
return;
}
int count = metadataBufferResult.getMetadataBuffer().getCount();
Log.d(LOG_TAG, "Successfully ran query for " + FILE_NAME + " and found " +
count + " results");
if (count > 1) {
Log.e(LOG_TAG, "App folder contains more than one database file! " +
"Found " + count + " matching results.");
return;
}
// Create the database on Google Drive if it doesn't exist already
if (count == 0) {
Log.d(LOG_TAG, "No existing database found on Google Drive");
saveToDrive(googleApiClient);
}
}
});
}
private static void saveToDrive(final GoogleApiClient googleApiClient) {
Log.d(LOG_TAG, "Starting to save to drive...");
// Create content from file
Drive.DriveApi.newDriveContents(googleApiClient).setResultCallback(
new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
if (!driveContentsResult.getStatus().isSuccess()) {
Log.w(LOG_TAG, "Drive contents result not a success! " +
"Not saving data to drive.");
return;
}
Log.d(LOG_TAG, "Created drive contents for file");
createNewFile(googleApiClient, driveContentsResult.getDriveContents());
}
});
}
private static void createNewFile(GoogleApiClient googleApiClient, DriveContents driveContents) {
// Write file to contents (see http://stackoverflow.com/a/33610727/4230345)
File file = new File(DATABASE_PATH);
OutputStream outputStream = driveContents.getOutputStream();
try {
InputStream inputStream = new FileInputStream(file);
byte[] buf = new byte[4096];
int c;
while ((c = inputStream.read(buf, 0, buf.length)) > 0) {
outputStream.write(buf, 0, c);
outputStream.flush();
}
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOG_TAG, "Written file to output stream of drive contents");
// Create metadata
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setTitle(FILE_NAME)
.setMimeType(MIME_TYPE)
.build();
// Create the file on Google Drive
DriveFolder folder = Drive.DriveApi.getAppFolder(googleApiClient);
folder.createFile(googleApiClient, metadataChangeSet, driveContents).setResultCallback(
new ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(@NonNull DriveFolder.DriveFileResult driveFileResult) {
if (!driveFileResult.getStatus().isSuccess()) {
Log.w(LOG_TAG, "File did not get created in Google Drive!");
return;
}
Log.i(LOG_TAG, "Successfully created file in Google Drive");
}
});
}
不同。
我想知道我是否能够利用API的修改处理(解释here),如果设备没有互联网连接,则更改排队等待上传。< / p>
答案 0 :(得分:2)
根据this answer,Google云端硬盘的Android API已经为您处理差异计算。所以没有必要做任何复杂的事情,只需使用API,就好像你完全重写了那个文件一样。
您可能还希望利用API透明脱机同步功能。