2015-05-08 69 views
0

請幫幫忙,我試圖找出是否存在服務器上的文件夾下面的方法GET DRIVEFOLDER谷歌驅動

我初始化並連接

mGoogleApiClient = new GoogleApiClient.Builder(AMain.this) 
.addApi(Fitness.CONFIG_API) 
.addApi(Drive.API) 
.addScope(Drive.SCOPE_FILE) 
.addConnectionCallbacks(AMain.this) 
.addOnConnectionFailedListener(AMain.this) 
.build(); 

mGoogleApiClient.connect(); 

連接建立

@Override 
public void onConnected(Bundle connectionHint) { 
    DriveFolder appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient); 
    DriveFolder myFldr = search(appFolder, "AMain"); 
} 

繼續嘗試查找文件夾

private static DriveFolder search(DriveFolder appFolder, String folder) { 
    Query query = new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, folder)).build(); 
    MetadataBuffer metadataBuffer = appFolder.queryChildren(mGoogleApiClient, query).await().getMetadataBuffer(); 
    if(metadataBuffer != null&&metadataBuffer.getCount()>0) { 
     Log.v("ME","Found "+metadataBuffer.getCount()+" existing folders"); 
     Metadata metadata = metadataBuffer.get(0); 
     if (metadata != null && metadata.isFolder()) { 
      Log.v("ME","Returning existing folder"); 
      return Drive.DriveApi.getFolder(mGoogleApiClient, metadata.getDriveId()); 
     }else{ 
      Log.v("ME","Returning created folder even though we found meta data"); 
      return appFolder.createFolder(mGoogleApiClient, new MetadataChangeSet.Builder().setTitle(folder).build()).await().getDriveFolder(); 
     } 
    }else{ 
     Log.v("ME","Returning created folder"); 
     return appFolder.createFolder(mGoogleApiClient, new MetadataChangeSet.Builder().setTitle(folder).build()).await().getDriveFolder(); 
    } 
} 

但在第二行收到錯誤

05-08 17:06:19.261 16538-16538/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: ru.moore.aisuchettehniki, PID: 16538 
    java.lang.NullPointerException: Attempt to invoke interface method 'com.google.android.gms.common.api.PendingResult com.google.android.gms.drive.DriveFolder.queryChildren(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.drive.query.Query)' on a null object reference 
      at ru.moore.aisuchettehniki.AMain.search(AMain.java:147) 
      at ru.moore.aisuchettehniki.AMain.onConnected(AMain.java:142) 
      at com.google.android.gms.common.internal.zzl.zzj(Unknown Source) 
      at com.google.android.gms.common.api.zzd.zzie(Unknown Source) 
      at com.google.android.gms.common.api.zzd.zzc(Unknown Source) 
      at com.google.android.gms.common.api.zzd$6.onConnected(Unknown Source) 
      at com.google.android.gms.common.internal.zzl.zzj(Unknown Source) 
      at com.google.android.gms.common.internal.zzl.zzfe(Unknown Source) 
      at com.google.android.gms.common.internal.zzk$zzf.zzje(Unknown Source) 
      at com.google.android.gms.common.internal.zzk$zza.zzc(Unknown Source) 
      at com.google.android.gms.common.internal.zzk$zza.zzi(Unknown Source) 
      at com.google.android.gms.common.internal.zzk$zzc.zzjg(Unknown Source) 
      at com.google.android.gms.common.internal.zzk$zzb.handleMessage(Unknown Source) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:211) 
      at android.app.ActivityThread.main(ActivityThread.java:5321) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 

回答

2

您需要提供另一個範圍訪問「app文件夾」。

地址:

 .addScope(Drive.SCOPE_APPFOLDER) 

到GoogleApiClient.Builder