0

Image not upload using MultipartEntity.

Gives status code 200 but image not updated on serverside.

 String responseBody;
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(
                    "http__zz/upload_picture?key=abc&property_id=10");

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM).toString()
                    + "/Camera/Test.jpg");
            ContentBody encFile = new FileBody(file, "image/png");

            entity.addPart("picture", encFile);

            request.setEntity(entity);

            ResponseHandler<String> responsehandler = new BasicResponseHandler();
            responseBody = client.execute(request, responsehandler);

            if (responseBody != null && responseBody.length() > 0) {
                Log.w("TAG", "Response image upload" + responseBody);

            }
3
  • please detail your issue. Error if any, expected result, actual result, method of debugging used, why would you expect a responseBody.length() > 0, ... There is just not enough information here to understand your problem Commented Sep 26, 2013 at 13:14
  • Gives status code 200 but image not updated on serverside. Commented Sep 26, 2013 at 13:16
  • still not enough info. Commented Sep 26, 2013 at 13:17

3 Answers 3

1

Try using a ByteArrayBody instead of a FileBody:

File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM).toString()
                    + "/Camera/Test.jpg");
Bitmap b = BitmapFactory.decodeFile(file;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 100, bao);

ByteArrayBody body = new ByteArrayBody(bao.toByteArray(), "image/jpeg", "picture");
entity.addPart("picture", body);
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you try to send it as base64 encoded string?

Comments

0

the best way to do this is to implement an IntentService and notify status with broadcast intents. please check out this code from git its work for me

https://github.com/alexbbb/android-upload-service

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.