0

I want to send an image captured by camera to a server, which creates blob key. I am not getting how to send that image to the server. In which format is the image is sent?

I am trying to send parameters through HttpParams.

This is my code but the data is not going to server. What is the problem?

String name=tname.getText().toString();
String addr=taddr.getText().toString();
String age=tage.getText().toString();
String cnct=tcnct.getText().toString();
String gen=tgen.getText().toString();
String wtm=twtm.getText().toString();
ba1=Base64.encodeToString(imageform, 0);
Date d=new Date();
String date=d.toString();

InputStream i1;
String back="";
HttpParams p=new BasicHttpParams();

p.setParameter("vname",name);
p.setParameter("address", addr);
p.setParameter("age", age);
p.setParameter("contact", cnct);
p.setParameter("gender", gen);
p.setParameter("whomto", wtm);

p.setParameter("myFile", ba1);

try {
    HttpClient httpclient = new DefaultHttpClient(p);

    HttpPost res=new HttpPost(result);
    HttpResponse response = httpclient.execute(res);

    HttpEntity entity = response.getEntity();
    i1 = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(i1,"iso-8859-1"),8);
    StringBuilder sb = new StringBuilder();


    if ( reader.readLine() == null) {
        Log.e("inside if","No data");
    } else {
        String line = reader.readLine();
        i1.close();
        back=sb.toString();
    }

I am not getting any errors or exceptions.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38

2 Answers2

0

You should make an MultipartPost and add the file to your MultipartEntity as follows :

 multipartEntity.addPart("data", new FileBody(capturedImagePath));

You should have a look a this answer Multipart post with Android for a more detailed answer.

Community
  • 1
  • 1
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • The problem is i am not storing the image as a file.I am just capturing the image and displaying on the imageview..Can I send that as a file in multipart entity??? – Rohan Deokar Aug 08 '12 at 06:04
  • ThIS MULTIPARTENTITY is working fine.You can even send the file directly to server through multipartEntity.I want to add one more thing that if you want to send byte to the server just convert it Base64 string and send it to server through parameters. – Rohan Deokar Aug 31 '12 at 06:42
0

Encode the image, using Base64, to a String and send it using MultipartEntity. In php retrieve the string an unpack it with base64_decode to the image. Check this question: https://stackoverflow.com/questions/10145417/android-send-image-through-http-post

Community
  • 1
  • 1
sabz23
  • 11
  • 1