Reputation: 41
I have created an app to try out Android SDK. It worked for me previously (getting authentication token, taking a picture and upload to Google Drive folder). Recently, my app has problem uploading stuff to Google Drive. Below is the piece of code that does the uploading, it basically check if the file has existed or not and decides to use insert() or update(). It worked for me before, but now when I try to upload files to Google Drive. After I have successfully create a folder in the GoogleDrive, then the next step is to upload the file, but every time I will get error message saying:
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: [*/*]"
My original guess was that I forgot to set MIMETYPE for the upload body in the code, is I updated my code and set mimetype before execute. In the Log, I can see that the mimetype has changed, however, when I execute the upload, I still got the same error message telling me that the Media type '' is not supported. (or it thinks it's empty). Anyone knows what could have happened? I searched previous questions, but they did help.
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;
}
anyone knows how to solve this problem? thank you~
Upvotes: 2
Views: 1472
Reputation: 302
The first line :
FileContent mediaContent = new FileContent("", localFile);
It contains a null string, instead of the mime type as in the format 'type/subtype' as 'image/jpeg'. this should be done before executing the request using insert. please do so and you won't be presented with a 400 Error code (Bad Request).
Upvotes: 0
Reputation: 3425
Make sure you include
<data android:mimeType="application/vnd.google-apps.drive-sdk.123456yourclientid" />
in your xml. Where 123456yourclientid is your client id from the Google API console.
The video on this page was very helpful Quickstart: Run a Drive App on Android
but also make sure you watch the follow up video on Integrate with the Android Drive App
Hope this helps.
Upvotes: 0
Reputation: 1
You seem to be setting the mime type in the File object, but you need to set it in the FileContent object.
When you create your file content, you can try passing in the type in
the constructor new FileContent("image/jpeg", localFile);
or using the method setType("image/jpeg");
but in your mediaContent instance, not in your body instance.
Upvotes: 0