我创建了一个应用程序来试用 Android SDK。它以前对我有用(获取身份验证令牌,拍照并上传到 Google Drive 文件夹)。最近,我的应用无法将内容上传到 Google 云端硬盘。下面是上传的一段代码,它基本上检查文件是否存在并决定使用 insert() 或 update()。它以前对我有用,但现在当我尝试将文件上传到 Google Drive 时。在我在 GoogleDrive 中成功创建文件夹后,下一步就是上传文件,但每次我都会收到错误消息:
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;
}
有谁知道如何解决这个问题?谢谢~