2013-09-26 47 views
0

我在谷歌驅動器中創建了一個文檔。我想使用谷歌驅動器android sdk上傳相同文件的新版本。我試過這樣的代碼,更新谷歌驅動器原生文檔的新版本

try{ 
                      // First retrieve the file from the API. 
    File file = service.files().get(fileId).execute(); 

java.io.File fileContent = new java.io.File("sdcard0/temp/test.doc"); 
FileContent mediaContent = new FileContent("application/vnd.google-apps.document", fileContent); 

File updatedFile = service.files().update(getID(), file, mediaContent).execute();         
} catch (IOException e1) { 
     Log.d("","An error occurred: " + e1); //No i18n 
} catch (Exception e){ 
     Log.d("","EXCEPTION IN SAVING"+e); //No i18n 
} 

但內容看起來像損壞在docs.google.com像enter image description here

請指引我,如果我做錯了什麼。

注意:相同的代碼適用於上傳的文檔。

回答

0

您不能將這些修訂用於像Google文檔這樣的原生格式。那些有他們自己的apis來修改它們。例如,電子表格包含電子表格提要api。

+0

可我知道如何做到這一點? – vignesh

+0

請參閱.google文檔列表api –

0

您可以使用update request中的convert參數爲Google Docs/Spreadsheet格式創建新版本。

繼修改代碼以使轉化率,同時上傳(未經測試,但相信這是正確的)

try{ 
    // First retrieve the file from the API. 
    File file = service.files().get(fileId).execute(); 

    java.io.File fileContent = new java.io.File("sdcard0/temp/test.doc"); 
    FileContent mediaContent = new FileContent("application/msword", fileContent); //Changed the mime type to original 

    File updatedFile = service.files().update(getID(), file, mediaContent) 
         .setConvert(true) //Convert the file while uploading 
         .execute();         
} catch (IOException e1) { 
     Log.d("","An error occurred: " + e1); //No i18n 
} catch (Exception e){ 
     Log.d("","EXCEPTION IN SAVING"+e); //No i18n 
} 
+0

我試過了您的代碼。但它仍然不工作..得到與上述相同的結果.. – vignesh

+0

我認爲這是android代碼,但你能證實嗎?我會嘗試編寫一個測試應用程序。我在我的Android應用程序中使用了類似的代碼,但以txt文件作爲源代碼,並且文件已正確轉換(插入,可能存在轉換更新的錯誤) – smokybob

+0

是的。這是android代碼。 – vignesh

相關問題