Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send image (bitmap) to server in android with multipart-form data json

Tags:

android

I have code to upload image to server and it works ,

HttpEntity resEntity;

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost post = new HttpPost(Constants.url_create_product);
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                File file= new File(path);
                FileBody bin = new FileBody(file);

                reqEntity.addPart("phone", new StringBody(mPhoneNumber));
                reqEntity.addPart("prod_title", new StringBody(namapro));
                reqEntity.addPart("prod_price", new StringBody(hargapro));
                reqEntity.addPart("prod_desc", new StringBody(despro));
                reqEntity.addPart("prod_order", new StringBody(orderpro));
                reqEntity.addPart("prod_image", bin);

                post.setEntity(reqEntity);
                    HttpResponse response = httpClient.execute(post);
                resEntity = response.getEntity();
                String response_str = EntityUtils.toString(resEntity);
                Gson gson = new Gson(); 
                gson.toJson(response_str);
                 if (resEntity != null) {
                     Log.i("RESPONSE",response_str);
                     runOnUiThread(new Runnable(){
                            public void run() {
                                 try {
                                    Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                               }
                        });
                 }

But i have menu image editor . that editor is crop image and thats code return bitmap value like this

Bundle extras = data.getExtras();

                if (extras != null) {               
                    photo = extras.getParcelable("data");

                    mImageView.setImageBitmap(photo);
                }

                File f = new File(mImageCaptureUri.getPath());            

                if (f.exists()) f.delete();

                break;

I wanna ask , how solution to send image to server with parameter bitmap. As you know my code to send image now use parameter path (string).

like image 297
ya Lya Avatar asked Oct 09 '12 09:10

ya Lya


1 Answers

ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

Then send this encodedImage as a String and in your server you will need to decode it in order to get the image itself.

like image 118
Carnal Avatar answered Sep 28 '22 16:09

Carnal