2013-07-03 84 views
2

我已經創建了一個應用來試用Android SDK。它以前爲我工作(獲取身份驗證令牌,拍照並上傳到Google雲端硬盤文件夾)。最近,我的應用在將內容上傳到Google雲端硬盤時出現問題。下面是上傳的代碼片段,它基本上檢查文件是否存在,並決定使用insert()或update()。它以前適用於我,但現在當我嘗試將文件上傳到Google Drive時。我已經成功地創建了Google雲端硬盤的文件夾後,再下一步就是要上傳的文件,但我每次都會得到錯誤消息時說:Google Drive sdk for android,在將文件上傳到Google Drive時出現錯誤400錯誤請求

07-03 13:49:54.824: W/System.err(31132): com.google.api.client.googleapis.json. 
GoogleJsonResponseException: 400 Bad Request 
07-03 13:49:54.824: W/System.err(31132): { 
07-03 13:49:54.824: W/System.err(31132): "code": 400, 
07-03 13:49:54.824: W/System.err(31132): "errors": [ 
07-03 13:49:54.824: W/System.err(31132):  { 
07-03 13:49:54.824: W/System.err(31132):  "domain": "global", 
07-03 13:49:54.824: W/System.err(31132):  "message": "Media type '' is not supported. Valid media types: [*/*]", 
07-03 13:49:54.824: W/System.err(31132):  "reason": "badContent" 
07-03 13:49:54.824: W/System.err(31132):  } 
07-03 13:49:54.824: W/System.err(31132): ], 
07-03 13:49:54.824: W/System.err(31132): "message": "Media type '' is not supported. Valid media types: [*/*]" 

我最初的猜測是,我忘了設定MIMETYPE爲上傳身體的代碼,我是否更新我的代碼,並在執行前設置mimetype。在日誌中,我可以看到mimetype已經改變,但是,當我執行上傳時,我仍然收到相同的錯誤消息,告訴我Media Type「'不受支持。 (或者認爲它是空的)。任何人都知道會發生什麼?我搜查了以前的問題,但他們確實有幫助。

private com.google.api.services.drive.model.File processGDFile(Drive service, 
    String parentId, File localFile) throws Exception { 
Log.i(TAG, "We are in processGDFile"); 
boolean existed = false; 
com.google.api.services.drive.model.File processedFile; 
// Determine whether the file exists in this folder or not. 
String q = "'" + parentId + "' in parents and" + "title = " + "'" 
    + localFile.getName() + "'"; 

FileContent mediaContent = new FileContent("", localFile); 

FileList resultFileList = ExcuteQuery(q); 

try { 
    // if this file already exists in GD, we do update 
    if (!resultFileList.getItems().isEmpty()) { 
    Log.i(TAG, "the file exists, use update...."); 
    existed = true; 
    com.google.api.services.drive.model.File gdFile = resultFileList 
     .getItems().get(0); 
    Log.i(TAG, "MIMETYPE:" + gdFile.getMimeType()); 
    gdFile.setMimeType("image/jpeg"); 
    Log.i(TAG, "MIMETYPE_after:" + gdFile.getMimeType()); 

    processedFile = service.files() 
     .update(gdFile.getId(), gdFile, mediaContent).execute(); 


    // Uncomment the following line to print the File ID. 
    Log.i(TAG, "Processed File ID: %s" + processedFile.getId()); 

    } else { 
    Log.i(TAG, "the file is new, create its meta data"); 
    // Start the new File's metadata. 
    com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File(); 
    body.setTitle(localFile.getName()); 


    // Set the parent folder. 
    if (parentId != null && parentId.length() > 0) { 
     body.setParents(Arrays.asList(new ParentReference().setId(parentId))); 
    } 
    Log.i(TAG, " before insert body"); 

    Log.i(TAG, "MIMETYPE:" + body.getMimeType()); 
    body.setMimeType("image/jpeg"); 
    Log.i(TAG, "MIMETYPE_after:" + body.getMimeType()); 
    processedFile = service.files().insert(body, mediaContent).execute(); 

    Log.i(TAG, "Processed File ID: %s" + processedFile.getId()); 

    } 
} catch (Exception e) { 
    throw e; 
} 
return processedFile; 

}

有誰知道如何解決這個問題?謝謝〜

回答

0

您似乎在File對象中設置了mime類型,但您需要將其設置在FileContent對象中。

當您創建文件的內容,你可以嘗試傳入型 構造new FileContent("image/jpeg", localFile);或使用方法setType("image/jpeg");但在你的mediaContent實例,而不是在你的身體例如

0

第一行:

FileContent mediaContent = new FileContent("", localFile); 

它包含空字符串,代替的mime類型作爲格式「類型/子類型」爲「圖像/ JPEG」。 這應該在使用插入執行請求之前完成。請這樣做,並且不會出現400錯誤代碼(錯誤請求)。

相關問題