PrvN
PrvN

Reputation: 2395

How to upload image using MultipartEntity in Android HttpPost?

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);

            }

Upvotes: 0

Views: 3145

Answers (3)

Sahir Saiyed
Sahir Saiyed

Reputation: 530

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

Upvotes: 0

Lovis
Lovis

Reputation: 10047

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);

Upvotes: 1

niegus
niegus

Reputation: 1828

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

Upvotes: 0

Related Questions