未使用 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() 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...