12

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

0

2 Answers 2

38
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.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry , iam newbie , so how server to decode it in order to get the image itself??
depends on what language you're using on your server. PHP has built in method for this. Just google the language on server + decode image string.
@carnal, i am getting image url is http://........exam1.jpg and i converted to bitmap but not for this photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
That is base64 not multipart!
5

*use three library
1.httpclient-4.3.6
2.httpcore-4.3.3
3.httpmime-4.3.6

 byte[] data = null; try { String url = "http://andapp.freetings.in/testbyme.php?"; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); MultipartEntity entity = new MultipartEntity(); if(imageBitmap!=null){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); imageBitmap.compress(CompressFormat.JPEG, 100, bos); data = bos.toByteArray(); entity.addPart("uplod_img", new ByteArrayBody(data,"image/jpeg", "test2.jpg")); } // entity.addPart("category", new StringBody(categoryname,"text/plain",Charset.forName("UTF-8"))); entity.addPart("category", new StringBody(catid,"text/plain",Charset.forName("UTF-8"))); entity.addPart("your_contact_no", new StringBody(phone,"text/plain",Charset.forName("UTF-8"))); entity.addPart("your_emailid", new StringBody(email,"text/plain",Charset.forName("UTF-8"))); entity.addPart("your_name", new StringBody(name,"text/plain",Charset.forName("UTF-8"))); httppost.setEntity(entity); HttpResponse resp = httpclient.execute(httppost); HttpEntity resEntity = resp.getEntity(); String string=EntityUtils.toString(resEntity); // Log.e("sdjkfkhk", string); return resEntity; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

2 Comments

Unable to find ByteArrayBody
u have need add three jar file those are httpclient-4.1.jar, httpcore-4.1.jar and httpmime-4.1.jar in libs folder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.