我正在开发一个Android应用程序,我正在我的应用程序中集成谷歌驱动器。我正在使用Eclipse IDE开发此应用程序。以下是我的代码
private void saveFiletoDrive() {
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveContentsResult driveContentsResult) {
if (!driveContentsResult.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new Drive backup.");
Toast.makeText(mContext, "Error on loading Google Drive. Retry", Toast.LENGTH_SHORT).show();
return;
}
Log.i(TAG, "Backup to drive started.");
try {
File dbFile = new File(inFileName);
FileInputStream inStream = new FileInputStream(dbFile);
ZipOutputStream outStream = new ZipOutputStream(new BufferedOutputStream(driveContentsResult.getDriveContents().getOutputStream()));
outStream.putNextEntry(new ZipEntry(inFileName));
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
/*outStream.close();
inStream.close();*/
//drive file Metadata
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setTitle("database_backup.db")
.setMimeType("application/db")
.build();
// Create an intent for the file chooser, and start it.
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(driveContentsResult.getDriveContents())
.build(mGoogleApiClient);
startIntentSenderForResult(intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
});
}
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_CREATOR:
// Called after a file is saved to Drive.
if (resultCode == RESULT_OK) {
Log.i(TAG, "Backup successfully saved.");
Toast.makeText(this, "Backup successufly loaded!", Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_CODE_OPENER:
if (resultCode == RESULT_OK) {
DriveId driveId = data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
//Toast.makeText(this, driveId.toString(), Toast.LENGTH_SHORT).show();
DriveFile file = driveId.asDriveFile();
importFromDrive(file);
}
}
}
我得到的Logcat消息是
Google Drive Activity(13699): GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{41f257d8: android.os.BinderProxy@41f25778}, message=null}
我在清单文件中添加了权限,甚至启用了Google Drive API。有人请告诉我这个解决方案