1

The whole thing works perfectly, except image won't show, no errors, Using RoR. What am I missing? All called by async class btw. Been trying several different methods with no avail, if someone could help me out that would be great. Willing to post more if needed.

Thanks!

public static void multiPart(Bitmap image, String topicid, String topost, Context c){
        String responseString = "";
        {
            try {
                String imageName = System.currentTimeMillis() + ".jpg";
                HttpClient httpClient = new MyHttpClient(c);
                HttpPost postRequest = new HttpPost("https://urlofmyapi");
                if (image==null){
                    Log.d("TAG", "NULL IMAGE");
                }
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                image.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, imageName);
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("feed", new StringBody(topost));
                reqEntity.addPart("post_to", new StringBody(topicid));
                reqEntity.addPart("upload_file", bab);
                postRequest.setEntity(reqEntity);
                HttpResponse response = httpClient.execute(postRequest);
                BufferedReader reader = new BufferedReader(
                  new InputStreamReader(
                      response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) {
                   s = s.append(sResponse);
                }
                responseString = s.toString();
              System.out.println("Response: " + responseString);
            } catch (Exception e) {
                Log.e(e.getClass().getName(), e.getMessage());
            }
          }
        }
user824015
  • 89
  • 3
  • 12
  • Are you sure its posting correctly? It's likely that its not transmitting the binary image blob portion correctly. Also, is it setting the content-length header? If possible, you should inspect the raw http post. – Eugene Mar 29 '12 at 22:53
  • how should I check out the raw http post? When I post without the image it works fine. Thanks – user824015 Mar 29 '12 at 23:11

2 Answers2

2

Perhaps you can do following step to import library into your Android.

requirement library:

  • apache-mime4j-0.6.jar
  • httpmime-4.0.1.jar

    1. Right click your project and click properties
    2. select java build path
    3. select tab called "Order and Export"
    4. Apply it
    5. Fully uninstall you apk file with the adb uninstall due to existing apk not cater for new library
    6. install again your apk
    7. run it

Thanks,

Jenz

Marko
  • 20,385
  • 13
  • 48
  • 64
jenz
  • 21
  • 2
0

HTTP POSTing images from Java to RoR always seems to have undue issues for me. Have you tried attaching the binary as a org.apache.http.entity.mime.content.FileBody object, like this Android Multipart Upload question?

Community
  • 1
  • 1
HoratioCain
  • 915
  • 9
  • 19