2017-03-19 47 views
0

我試圖上傳,然後分享谷歌文件在android系統驅動。我能夠獲得它的文件ID和資源ID。現在,我希望把它公開,以便用戶有鏈接可以訪問它。我如何以編程方式執行此操作?我想我在谷歌文件推動公共

回答

0

使用Permissions.create和設置文件到「人」的類型。 下面是來自Manage Sharing in Drive引導代碼片段:

String fileId = "1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ"; 
JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>() { 
    @Override 
    public void onFailure(GoogleJsonError e, 
          HttpHeaders responseHeaders) 
      throws IOException { 
     // Handle error 
     System.err.println(e.getMessage()); 
    } 

    @Override 
    public void onSuccess(Permission permission, 
          HttpHeaders responseHeaders) 
      throws IOException { 
     System.out.println("Permission ID: " + permission.getId()); 
    } 
}; 
BatchRequest batch = driveService.batch(); 
Permission userPermission = new Permission() 
     .setType("user") 
     .setRole("writer") 
     .setEmailAddress("[email protected]"); 
driveService.permissions().create(fileId, userPermission) 
     .setFields("id") 
     .queue(batch, callback); 

Permission domainPermission = new Permission() 
     .setType("domain") 
     .setRole("reader") 
     .setDomain("example.com"); 
driveService.permissions().create(fileId, domainPermission) 
     .setFields("id") 
     .queue(batch, callback); 

batch.execute(); 
+0

我已經嘗試過這樣做。我得到這個錯誤說:嘗試在空對象引用調用虛擬方法com.google.api.client.googleapis.batch.BatchRequest com.google.api.services.drive.Drive.batch()'。我該怎麼辦? –

相關問題