如何解决未使用 Google Drive API 调用 onActivityResult
我正在尝试使用 Google Drive API 将文件上传到 Drive。但是,在我做之后的下面的代码中
startActivityForResult(signInIntent,400);
它永远不会到达 onActivityResult,它只是直接使用 null driveServiceHelper 转到 uploadPdfFile()。
我已经调查了我在这里发现的几件事情,但这似乎是一个不同的问题?我是 Android 的初学者,所以也许我忽略了一些东西?谢谢。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
}
});
requestSignIn();
uploadPdfFile();
}
private void uploadPdfFile() {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Uploading to Google Drive");
progressDialog.setMessage("Please wait...");
progressDialog.show();
String filePath = "/storage/emulated/0/mypdf.pdf";
driveServiceHelper.createFilePDF(filePath)
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),"Upload successfully",Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),"Check your drive api key",Toast.LENGTH_LONG).show();
}
});
}
DriveServiceHelper driveServiceHelper;
private void requestSignIn() {
GoogleSignInoptions signInoptions = new GoogleSignInoptions.Builder(GoogleSignInoptions.DEFAULT_SIGN_IN)
.requestemail()
.requestScopes(new Scope(DriveScopes.DRIVE_FILE))
.build();
GoogleSignInClient client = GoogleSignIn.getClient(this,signInoptions);
Intent signInIntent = client.getSignInIntent();
startActivityForResult(signInIntent,400);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode) {
case 400:
if (resultCode == RESULT_OK) {
handleSignalInIntent(data);
}
break;
}
}
private void handleSignalInIntent(Intent data) {
GoogleSignIn.getSignedInAccountFromIntent(data)
.addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() {
@Override
public void onSuccess(GoogleSignInAccount googleSignInAccount) {
GoogleAccountCredential credential = GoogleAccountCredential
.usingOAuth2(MainActivity.this,Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccountName(googleSignInAccount.getAccount().name);
Drive googleDriveService = new Drive.Builder(
AndroidHttp.newCompatibleTransport(),new GsonFactory(),credential)
.setApplicationName("My Drive Tutorial")
.build();
driveServiceHelper = new DriveServiceHelper(googleDriveService);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
这几乎是项目中的所有代码。 (除了 DriveServiceHelper)
解决方法
Root cause:
首先,您正在调用 requestSignIn
方法。一旦第 startActivityForResult(signInIntent,400);
行执行,
控制转到 uploadPdfFile
方法调用。那时您的 driveServiceHelper
未初始化。
为了解决这个问题,在调用 uploadPdfFile
方法之前,首先你必须检查你是否已经在唱歌。如果已经登录,则只调用 uploadPdfFile
方法。否则调用 requestSignIn
方法。
在这一行之后
driveServiceHelper = new DriveServiceHelper(googleDriveService);
uploadPdfFile()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。