onActivityResult 未使用 Google Drive API 调用
onActivityResult not called using Google Drive API
我正在尝试使用 Google 驱动器 API 将文件上传到驱动器。但是,在我下面的代码中
startActivityForResult(signInIntent, 400);
它永远不会到达 onActivityResult,它只是使用空 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()
我正在尝试使用 Google 驱动器 API 将文件上传到驱动器。但是,在我下面的代码中
startActivityForResult(signInIntent, 400);
它永远不会到达 onActivityResult,它只是使用空 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()