2013-07-03 69 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错误代码(错误请求)。

相关问题