Upload image (Android)

582 views
Skip to first unread message

Jairam

unread,
Dec 21, 2012, 2:54:50 AM12/21/12
to tumbl...@googlegroups.com
I can't seem to figure out how to upload an image to Tumblr via Java (Android). My code seems to be failing when I try to add the image as a NameValuePair. I know it's failing here because I can submit a text post just fine.

Here is what I have:

HttpPost hpost = new HttpPost("http://api.tumblr.com/v2/blog/myblog.tumblr.com/post");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("type", "photo"));
nameValuePairs.add(new BasicNameValuePair("caption", "hello"));
nameValuePairs.add(new BasicNameValuePair("source", imageURI)); // imageURI is the uri of the local file on the device

// nameValuePairs.add(new BasicNameValuePair("data", imageConvertedToByteArray));  This also doesn't work

I've searched on Google and nobody seems to have a clear answer on how to solve this. I've also tried using a MultipartEntity but that doesn't work either.

The code above returns me a status code 400 with a message of "Bad Request". Any help is appreciated.

Michel Pelletier

unread,
Dec 21, 2012, 11:32:19 AM12/21/12
to tumbl...@googlegroups.com
Hello,

I too was stuck with being able to post text but not images.  I've got the solution for iOS/Objective-C, not Java/Android.  Hopefully my code will give you hints as to what's going on.  Here is the relevant code:

                     if (image == nil) {

                         postUri = [[NSString alloc]

                                    initWithFormat:@"%@/%@/post",

                                    TUMBLR_BLOG_FEED, [blogUrl host]];

                         postUrl = [NSURL URLWithString:postUri];

                         sParams = [[NSString alloc] initWithFormat:@"body=%@&type=text&title=Fused", message];

                         bodyData = [NSMutableData dataWithData:[sParams dataUsingEncoding:NSUTF8StringEncoding]];

                         postRequest = [NSMutableURLRequest requestWithURL:postUrl];

                         [postRequest setHTTPMethod:@"POST"];

                         [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

                         [postRequest setHTTPBody:bodyData];

                     } else {

                         postUri = [[NSString alloc]

                                    initWithFormat:@"%@/%@/post?type=photo&caption=%@",

                                    TUMBLR_BLOG_FEED, [blogUrl host], message];

                         postUrl = [NSURL URLWithString:postUri];

                         mimeBoundary = @"n1z2y3x4w5v6u7t";

                         sParams = [[NSString alloc] initWithFormat:@"type=photo&caption=%@\r\n\r\n", message];

                         bodyData = [NSMutableData dataWithData:[sParams dataUsingEncoding:NSUTF8StringEncoding]];

                         

                         [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", mimeBoundary]

                                           dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"Content-Disposition: form-data; name=\"data\"; filename=\"upload.jpg\"\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"Content-Type: image/jpeg\r\n\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:UIImageJPEGRepresentation(image, 1.0f)];

                         [bodyData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                         

                         [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", mimeBoundary]

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"Content-Disposition: form-data; name=\"caption\"\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"Content-Type: text/plain; charset=utf-8\r\n\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[[NSString stringWithFormat:@"%@\r\n", message]

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         

                         [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", mimeBoundary]

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"Content-Disposition: form-data; name=\"type\"\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"Content-Type: text/plain; charset=utf-8\r\n\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         [bodyData appendData:[@"photo\r\n"

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         

                         [bodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n\r\n", mimeBoundary]

                                               dataUsingEncoding:NSUTF8StringEncoding]];

                         

                         postRequest = [NSMutableURLRequest requestWithURL:postUrl];

                         [postRequest setHTTPMethod:@"POST"];

                         [postRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", mimeBoundary]

                            forHTTPHeaderField:@"Content-type"];

                         [postRequest setValue:[NSString stringWithFormat:@"%u", bodyData.length]

                            forHTTPHeaderField:@"Content-length"];

                         [postRequest setHTTPBody:bodyData];

                     }


The first part of the "if" is for posting text.  The second part is for posting an image.  As you can see, the "Content-type" is indeed set to "multipart/form-data" with a mime boundary when posting an image.  Also, the URL to use for the post has the "?type=photo&caption=message" appended to it.  The body contains 3 sections, each separated by the mime boundary.  The first section is for the jpeg data, while the others repeat the same parameters as appended to the URL.

Hope this helps.

Michel.

Stumbler Dev

unread,
Jan 5, 2013, 7:20:03 AM1/5/13
to tumbl...@googlegroups.com
Have you been able to solve this issue? If so, we would like to hear your solution as we have also faced this problem.

source ("The photo source URL") refers to the location of an image that is available on the web and not a local file.


Reply all
Reply to author
Forward
0 new messages