1

I'm trying to upload an image using the following code:

  HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(
                    "http://konsole-data.de/uploadtest/upload.php");

            MultipartEntity multiPart = new MultipartEntity();
            multiPart.addPart("picture", new FileBody(new File(path)));

            httpPost.setEntity(multiPart);
            try {
                HttpResponse res = httpClient.execute(httpPost);

                            Toast.makeText(getApplicationContext(),res.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (ClientProtocolException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

path is a String which identifies the image like /mnt/sdcard/DCIM/12712.jpg The connection works BUT no image is uploaded to the server, you can see a debug file here: http://konsole-data.de/uploadtest/data/20121214-144802-.dbg
What am doing wrong?

Droidman
  • 11,485
  • 17
  • 93
  • 141

1 Answers1

2

You should probably specify the HttpMultipartMode, and the MIME type of the file (but this is not necessary i think):

MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

FileBody bin = new FileBody(new File(path), "image/jpeg");
multipart.addPart("picture", bin);

EDIT:

You should also check if you use the right path. Instead of creating the File object as an anonymous inner class:

File file = new File(path);
if(file.exists()){
    FileBody bin = new FileBody(file, "image/jpeg");
    multipart.addPart("picture", bin);
} else {
    Log.w(YourClass.class.getSimpleName(), "File " + path + " doesn't exist!");
}
Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • just added that.. Still can't get the image uploaded: POST: a:0:{} FILES: a:0:{} – Droidman Dec 14 '12 at 14:11
  • The code seems ok. I use the same method to upload pictures in one of my projects. Are you sure that you have the right path for the image? – Adam Monos Dec 14 '12 at 14:12
  • yep, I used Toast to check that. Path was mnt/sdcard/DCIM/Camera/162533.jpg – Droidman Dec 14 '12 at 14:28
  • That path is wrong. It's '/mnt/sdcard/...'. You should check with `File.exists()` if you are looking at the right places. – Adam Monos Dec 14 '12 at 14:31
  • thanks. The file exists, I forgot to say that I use the same path to create an image preview in the same activity, but I still can't manage the upload.. POST: a:0:{} FILES: a:0:{} Can the reason be that the php script cant handle the file correctly? Thought when I select an image from PC it works fine – Droidman Dec 14 '12 at 15:05
  • No clue, but the android side of the code works, I'm using the same code right now to upload images to our server, so there shouldn't be any problems. Are you setting the mime type too, like in my examples? – Adam Monos Dec 14 '12 at 15:17
  • yep. Does your server side use php or java to handle incoming connections? – Droidman Dec 14 '12 at 15:19
  • It uses java. We only had problems with it when we didnt set the mime type correctly. – Adam Monos Dec 14 '12 at 15:37
  • our test server uses a php script, but in the final version there's an JSON object that must be returned with certain http POST attributes.. – Droidman Dec 14 '12 at 16:17
  • Yes, I use this in my own project. – Adam Monos Apr 15 '13 at 09:31