0

First I am consverting user selected image to base 64 string then i am sending this image to php server, There i am not getting complete base64 string. I don't know whats the problem.

Here is code:-

 File file = new File(imagepath);
 //encodeImagetoString convert image to base64 string
 encodeImagetoString();
 //encodedString is a base64 string
 String imageString= encodedString; 
 EditText text= (EditText) rootView.findViewById(R.id.editText2);
 String shayaritext=text.getText().toString();
 Spinner mySpinner=(Spinner) rootView.findViewById(R.id.spinner1);
 String catValue = mySpinner.getSelectedItem().toString();
 UserFunctions userFunction = new UserFunctions();
 json = userFunction.uploadShayariData(shayariName,catValue,shayaritext, imageString);

uploadShayariData function:-

 @SuppressWarnings({ "deprecation", "deprecation" })
public JSONObject uploadShayariData(String name,String catValue,String shayaritext,String image){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", shayariUpload));
    params.add(new BasicNameValuePair("cat", catValue));
    params.add(new BasicNameValuePair("title", name));
    params.add(new BasicNameValuePair("data", shayaritext));
    params.add(new BasicNameValuePair("image", image));

    // getting JSON Object
    JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
    // return json
    return json;
    }

PHP code:-

  $image3=$_POST['image'];
  // Get file name posted from Android App
  $filename = "a.png";
 // Decode Image
 $binary=base64_decode($base);
 header('Content-Type: bitmap; charset=utf-8');
 // Images will be saved under 'www/imgupload/uplodedimages' folder
 $file = fopen('uploads/'.$filename, 'wb');
 // Create File
 fwrite($file, $binary);
 fclose($file);

1 Answers1

0

You can do this with a Multipart post request:(This way, you dont need to create json)

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverURL);
MultipartEntity postEntity = new MultipartEntity();
File file = new File("Your File path on SD card");
postEntity.addPart("fileupload", new FileBody(file, "image/jpeg"));
postEntity.addPart("loginKey", new StringBody(""+loginKey));
postEntity.addPart("message", new StringBody(message));
postEntity.addPart("token", new StringBody(token));
post.setEntity(postEntity);
response = client.execute(post);

You have to add this mime4j library. http://sourceforge.net/projects/mime4j/?source=dlp

Maria Gheorghe
  • 599
  • 1
  • 5
  • 18
  • Yes we can use multipart entity, But most of the code on my application written in JSON, And handling it on server, So i need to change most of the code for if i use multipart. – Preeti Gupta Nov 29 '14 at 08:05
  • I undstand ,My advice is ,if you want a stable solutionto dnt post big data like images and files thru json, it is not designed for this – Maria Gheorghe Nov 29 '14 at 08:08
  • look at this too ,if your only option is json : http://stackoverflow.com/questions/4083702/posting-a-file-and-data-to-restful-webservice-as-json – Maria Gheorghe Nov 29 '14 at 08:13
  • I updated my code to test. And now it's working with another code. Thanks – Preeti Gupta Nov 29 '14 at 09:05