我正在尝试打开一个文件,但它不会让我因某种原因构建intentsender。 mGoogleApiClient已被调用连接,但它仍然没有。我有一个不同的方法,用于上传文件,它的工作原理。但是,这段代码使用.onResultCallback。我试图使openDriveFile()尽可能简单,但它不允许我,我没有找到任何对我有用的实现。任何帮助表示赞赏。如果您需要我发布其他方法的代码,请询问。
public String openDriveFile(){
final String[] contentsAsString = new String[1];
if(mGoogleApiClient.isConnected()){
System.out.println("IS CONNECTED!!!!!");
}else{
//connect it
System.out.println("ISNT CONNECTED!!!!!");
mGoogleApiClient.connect();
}
IntentSender is = Drive.DriveApi.newOpenFileActivityBuilder().setMimeType(new String[] { "text/plain", "text/html" }).build(mGoogleApiClient);
try {
startIntentSenderForResult(
is, 1, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
return contentsAsString[0];
}
这是我目前的代码。但是我收到了这个错误:
04-16 19:00:45.156 20896-20896/david.projectclouds E/AndroidRuntime:
FATAL EXCEPTION: main
Process: david.projectclouds, PID: 20896
java.lang.IllegalStateException: Client must be connected
at com.google.android.gms.common.internal.zzaa.zza(Unknown Source)
at com.google.android.gms.drive.OpenFileActivityBuilder.build(Unknown Source)
at david.projectclouds.MainActivity.openDriveFile(MainActivity.java:297)
at david.projectclouds.MainActivity$3.onClick(MainActivity.java:182)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1045)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1219)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3191)
at android.widget.AbsListView$3.run(AbsListView.java:4139)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6217)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
您需要等待客户端实际连接。 Google API在连接时提供了一种回调方法。
将mGoogleApiClient.connect();
之后的所有代码移至此方法:
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected() called");
//...
}
答案 1 :(得分:0)
试试这个
if (isGooglePlayServicesAvailable()){
if (mGoogleApiClient == null){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
private boolean isGooglePlayServicesAvailable() {
final int PLAY_SERVICES_RESOLUTION_REQUEST =9 *1000;
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(this);
if (result !=ConnectionResult.SUCCESS) {
if (googleAPI.isUserResolvableError(result)) googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show();
return false;
}
return true;
}
答案 2 :(得分:0)
我发现了我的问题。在使用我的功能之前,我似乎必须使用Google身份验证(即使我已登录)。我不知道我是怎么错过这个的,我绝对不能这样离开。