2015-01-01 21 views
2

我有一個Android應用程序,允許用戶將.csv文件導出到Google Drive,以便他們可以編輯它,然後將文件重新導入到Android中。使用Android SDK將Google文件上傳爲Google表格

這用於與舊版Google Docs API完美配合。我從去年的舊版API升級到Google Drive的早期版本,並且能夠很好地運行。現在,當我升級到最新版本時,我的應用程序的這一功能已被有效打破。我認爲這是SDK權限的組合,並從Google雲端硬盤分發Google表格。

會發生什麼事是我上傳一個文件,該元數據:

MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
                 .setTitle(fileName) 
                 .setMimeType("text/csv") 
                 .setStarred(true) 
                 .build(); 

那麼上傳的文件有一個藍色的谷歌文檔圖標。當用戶訪問Google雲端硬盤進行編輯時,他們只能「預覽」文檔。在他們預覽它時,他們可以使用Google表格「打開」文檔,然後創建一個新文檔(此新文檔具有綠色Google表格圖標),並且由於Google SDK具有此新的「功能」(引自:https://developers.google.com/drive/android/queries):

注意:Android Drive API僅適用於 https://www.googleapis.com/auth/drive.file範圍。這意味着只有 用戶已經打開或使用您的應用程序創建的文件可以是 與查詢匹配。

新文件有我的不能看到,所以用戶的編輯無法檢索。

有什麼方法可以直接將它作爲Google表格文件上傳?或者可能是我完全錯過的另一個解

編輯

這裏是我如何上傳使用谷歌驅動器的SDK我的代碼的例子:我創建了谷歌的客戶

在我的AsyncTask的構造函數:

mGoogleApiClient = new GoogleApiClient.Builder(activity) 
          .addApi(Drive.API) 
          .addScope(Drive.SCOPE_FILE) 
          .addConnectionCallbacks(this) 
          .addOnConnectionFailedListener(this) 
          .build(); 
mGoogleApiClient.connect(); 

然後在doInBackground方法中,我這樣做:

DriveApi.DriveContentsResult cResult = Drive.DriveApi.newDriveContents(mGoogleApiClient).await(); 

OutputStream os = cResult.getDriveContents().getOutputStream(); 
InputStream is = new FileInputStream(f); 

//write the input stream to the output stream 
.... 

DriveFileResult exportResult = Drive.DriveApi.getRootFolder(mGoogleApiClient) 
                  .createFile(mGoogleApiClient, changeSet, cResult.getDriveContents()) 
                  .await(); 

這很簡單(一旦你看着辦吧)那裏似乎沒有要離開告訴我想這個文件是一個「谷歌板」,而不是「谷歌文檔」

的SDK謝謝!

-Aaron

+0

我的情況是,該import = false(這是默認值)。 Set import = true – pinoyyid

+0

這是使用谷歌驅動器的網絡API?我沒有看到Android SDK(developers.google.com/drive/android)中的任何地方,讓您設置一個名爲import的屬性。我更新了問題以包含與Android SDK相同的代碼。 – Aaron

+1

正如@Rivero在下面提到的,要做到這一點,您需要使用Android API當前不支持的convert參數。您可以在API問題跟蹤器上提交功能請求,概述您的用例嗎? https://code.google.com/a/google.com/p/apps-api-issues/同時我建議繼續使用Java API客戶端。 – Daniel

回答

1

您應原轉換明確到電子表格中,否則會最終成爲一條直線上傳到雲端硬盤帳戶。嘗試以下代碼:

java.io.File fileContent = new java.io.File(csvfilepath); 
FileContent mediaContent = new FileContent("text/csv", fileContent); 

// File's metadata. 
File body = new File(); 
body.setTitle(fileContent.getName()); 
body.setMimeType("text/csv"); 

Insert request = service.files().insert(body, mediaContent); 
request.setConvert(true); 
File file = request.execute(); 
+1

我認爲這是google-api-client api不是嗎?谷歌正在要求Android開發者使用新的Drive SDK(https://developers.google.com/drive/android/)訪問谷歌驅動器,並帶有貶低和禁用威脅。 – Aaron

相關問題