在onActivityResult中,GoogleApiClient尚未连接,但未连接

时间:2017-10-02 04:48:59

标签: android android-intent google-api-client onactivityresult startactivityforresult

  

目前我正在使用Google的附近Api从图库发送图片。连接部分正在工作,当我从startActivityForResult获取图片并使用API​​发送图片时,我得到GoogleApiClient没有连接。 Idk为什么它在onActivityResult之前断开连接

10-02 10:08:13.486 10678-10678/com.blazeey.nearbycamera V/NearbyCamera: Activity Paused  
10-02 10:08:13.487 10678-10678/com.blazeey.nearbycamera V/NearbyCamera: Google ClientConnected  
10-02 10:08:13.711 10678-10736/com.blazeey.nearbycamera D/OpenGLRenderer: endAllStagingAnimators on 0xb396ab80 (RippleDrawable) with handle 0xb0667aa0  
10-02 10:08:18.790 10678-10678/com.blazeey.nearbycamera V/NearbyCamera: Google Client Not Connected  
10-02 10:08:18.928 10678-10678/com.blazeey.nearbycamera V/NearbyCamera: content://com.android.providers.media.documents/document/image%3A41490  
10-02 10:08:18.929 10678-10678/com.blazeey.nearbycamera W/System.err: java.lang.IllegalStateException: GoogleApiClient is not connected yet.  
10-02 10:08:18.958 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.google.android.gms.internal.zzbcd.zze(Unknown Source)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.google.android.gms.internal.zzbcx.zze(Unknown Source)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.google.android.gms.internal.zzbcp.zze(Unknown Source)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.google.android.gms.internal.zzclm.sendPayload(Unknown Source)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.blazeey.nearbycamera.MainActivity.send(MainActivity.java:569)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.blazeey.nearbycamera.MainActivity.onActivityResult(MainActivity.java:215)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7330)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3741)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3788)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.app.ActivityThread.-wrap16(ActivityThread.java)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.os.Looper.loop(Looper.java:148)  
10-02 10:08:18.959 10678-10678/com.blazeey.nearbycamera W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5471)  
10-02 10:08:18.960 10678-10678/com.blazeey.nearbycamera W/System.err:     at java.lang.reflect.Method.invoke(Native Method)  
10-02 10:08:18.960 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
10-02 10:08:18.960 10678-10678/com.blazeey.nearbycamera W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
10-02 10:08:18.961 10678-10678/com.blazeey.nearbycamera D/AutoManageHelper: onStart true {0=com.google.android.gms.internal.zzbau$zza@a5e1dc8}  
10-02 10:08:18.967 10678-10678/com.blazeey.nearbycamera V/NearbyCamera: Activity Resumed  
10-02 10:08:18.967 10678-10678/com.blazeey.nearbycamera V/NearbyCamera: Google Client Disconnected  
10-02 10:08:19.099 10678-10678/com.blazeey.nearbycamera V/CONNECTION :: Connected
  

这是代码

sendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent intent = new Intent();
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent,REQUEST_CODE);
    }
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

    if(!googleApiClient.isConnected()) {
        logV("Google Client Not Connected");
        googleApiClient.connect();
        }
    else {
        logV("Google Client Connected");
    }

    switch (requestCode)
    {
    case REQUEST_CODE:
    try {
        if (resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();
            ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");  
            Payload filePayload = Payload.fromFile(pfd);
            String payloadFilenameMessage = filePayload.getId() + ":" + uri.getLastPathSegment();

            logV(uri.toString());
            if(googleApiClient.isConnected())
                send(filePayload,getDiscoveredEndpoints());
                else {
                    googleApiClient.connect();
                    send(filePayload,getDiscoveredEndpoints());
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }

        break;
}
}


public void createGoogleApi() {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Nearby.CONNECTIONS_API)
                .addConnectionCallbacks(this)
                .enableAutoManage(this,this)
                .build();
    }
}

0 个答案:

没有答案