2017-05-07 26 views
0

我正在使用Google Drive API for android。這是我現在的代碼。openFileActivityBuilder意圖。如何等待它連接以及如何從中取出文件

public void downloadFromDrive(){ 

     mGoogleApiClient.connect(); 


     IntentSender intentSender = Drive.DriveApi.newOpenFileActivityBuilder().setMimeType(new String[]{"text/plain"}).build(mGoogleApiClient); 
    try{ 
     startIntentSenderForResult(intentSender,2,null,0,0,0); 
    } catch (IntentSender.SendIntentException e) { 
     e.printStackTrace(); 
     Log.w(TAG, "Unable to send intent", e); 

    } 

}

目前,有2個問題吧。由於沒有結果回調mGoogleClient不能與mGoogleApiClient.connect()連接。有關如何等待實際連接的任何想法?使用isConnected()或onConnect()只會讓我陷入無限循環。我現在只是先用不同的方法連接API客戶端(上傳東西的那個)

如何從IntentSender獲得FileStream?我有關於如何上傳到Google雲端硬盤的代碼,並且我從來不需要用任何方法指定我的OutputStreamWriter。我剛剛創建了一個輸出流,稱爲intentsender,GUI上傳文件沒有問題。

+0

嘗試使用['enableAutoManage(FragmentActivity,GoogleApiClient.OnConnectionFailedListener)'](https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient.Builder#enableAutoManage( android.support.v4.app.FragmentActivity,com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener)),這是管理連接的最簡單的方法,如[documentation](https:// developers)中所述。 google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient)。 – Teyam

+0

另請參閱[訪問Google API](https://developers.google.com/android/guides/api-client)瞭解更多信息。 – Teyam

+0

@Teyam似乎已經解決了這個問題。 – Nephilim

回答

0

問題的第一部分已經被上面評論中可愛的人解決了。 (enableAutoManage)

通過編輯OnActivityResult方法可以實現從IntentSender獲取文件流。

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    System.out.println("ONACTIVITY RESULT"); 
    System.out.println("REQUEST CODE" + requestCode); 
    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleSignInResult(result); 
     System.out.println("GOOGLE SIGN IN"); 
    } 
    if(requestCode == 2){ 

      try{ 
       mDriveID = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID); 
       System.out.println("ONACTIVITY DRIVE ID " + mDriveID); 

      } 
      catch (NullPointerException e) { 
      } 

    } 
} 

onActivityResults每當您完成GoogleDrive意圖(我想,不確定)時執行。因此,如果您爲其他任何事情啓動IntentSender,則必須發現錯誤。這不是最簡潔的答案,但它的工作。

相關問題