I am trying to use the Google Drive API to upload a file to Drive. However, in my code below after I do
startActivityForResult(signInIntent, 400);
It never reaches onActivityResult, it just goes directly to uploadPdfFile() with a null driveServiceHelper.
I have looked into several things I found around here, but this seems to be a different issue? I am a beginner with Android so maybe I am overlooking something? Thanks.
@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) {
}
});
}
This is pretty much all the code in the project. (apart from the DriveServiceHelper)
Root cause:Firstly You are calling
requestSignInmethod. As soon as linestartActivityForResult(signInIntent, 400);executed,control goes to
uploadPdfFilemethod call. At that time yourdriveServiceHelperis not initialized.To handle this, before calling
uploadPdfFilemethod, firstly you have to check whether you are already singin or not. If already signin then only calluploadPdfFilemethod. else callrequestSignInmethod. and after this line