我正在尝试在Google云端硬盘的Appdata文件夹中为我的应用程序创建电子表格文件,以将其用作远程数据库。
刚刚开始,我已经被卡住了。文件是正确创建的(?),但过了一会儿它们似乎消失了。
我这样做:
首先,在onCreate方法中,我登录我的Google帐户:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_APPFOLDER)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Drive.API)
.addConnectionCallbacks(this)
.build();
signIn();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivityResult处理更新UI(显示用户的姓名和个人资料照片,在登录/退出方面都很棒)
然后,在onConnected(Bundle bundle)方法中,我试着找出用户是否已经在AppFolder中有现有数据库,如果没有,我要求他创建一个。
@Override
public void onConnected(Bundle bundle) {
queryDrive();
}
public void queryDrive() {
Query query = new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, DreamDbSchema.NAME_REMOTE)).build();
Drive.DriveApi.query(mGoogleApiClient, query).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
if (buffer != null) {
if (buffer.getCount() == 0) {
new AlertDialog.Builder(ListActivity.this).setTitle(R.string.no_database_on_gdrive)
.setMessage(R.string.no_database_on_gdrive_message)
.setCancelable(false)
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(driveContentsCallback);
}
})
.setNeutralButton(R.string.cancel_and_disconnect, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
signOut();
}
})
.show();
} else {
for (int i = 0; i < buffer.getCount(); i++) {
Log.d("DRIVE_FILES: ", buffer.get(i).getTitle());
}
}
} else {
Log.d("DRIVE_BUFFER: ", "null");
}
}
});
}
final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(ListActivity.this, R.string.error_while_creating_GDrive_file, Toast.LENGTH_SHORT).show();
return;
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(DreamDbSchema.NAME_REMOTE)
.setMimeType("vnd.google-apps.spreadsheet")
.build();
Drive.DriveApi.getAppFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, result.getDriveContents())
.setResultCallback(fileCallback);
}
};
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(DriveFolder.DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(ListActivity.this, R.string.error_while_creating_GDrive_file, Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(ListActivity.this, getString(R.string.database_created_GD), Toast.LENGTH_SHORT).show();
}
};
一切似乎都运转了大约10分钟。在另一个活动中,我可以成功删除此文件:
@Override
public void onClick(View v) {
if (!mGoogleApiClient.isConnected()) {
Toast.makeText(SettingsActivity.this, R.string.not_loged_in, Toast.LENGTH_SHORT).show();
} else {
Drive.DriveApi.getAppFolder(mGoogleApiClient).listChildren(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(final DriveApi.MetadataBufferResult metadataBufferResult) {
Log.d("DRIVE_FILES: ", String.valueOf(metadataBufferResult.getMetadataBuffer().getCount()));
for (int i = 0; i < metadataBufferResult.getMetadataBuffer().getCount(); i++) {
final int j = i;
DriveId fileId = metadataBufferResult.getMetadataBuffer().get(i).getDriveId();
Drive.DriveApi.getFile(mGoogleApiClient, fileId).delete(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Toast.makeText(SettingsActivity.this, R.string.database_removed_GD, Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
它还会在日志“1”中打印一段时间。但是给它一个十分钟左右,我再次提示创建数据库,因为它发现Appdata文件夹为空。