Here is the code I wrote to send a binary file along some strings to a PHP web application server.
public void doRegister(final String userEmail, final String userPass, final File userPhotoId) {
HttpClient myHttpClient = new DefaultHttpClient();
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpPost registerTry = new HttpPost(Constants.oAuthRegURL);
MultipartEntity signupEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
signupEntity.addPart("email", new StringBody(userEmail, Charset.forName("UTF-8")));
signupEntity.addPart("password", new StringBody(userPass, Charset.forName("UTF-8")));
signupEntity.addPart("User[profilePicture]", new FileBody(userPhotoId, "image/jpeg"));
registerTry.setEntity(signupEntity);
HttpResponse signupHttpResp = myHttpClient.execute(registerTry);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
the file userPhotoId is a photo file created and returned via Camera API (i get the Data returned via Camera in onActivityResult & create a Bitmap from it & then create a File object from that Bitmap etc.)
so problem here is, the photo is not sent to the server this way. BUT, if i remove the mimetype in FileBody section like this:
signupEntity.addPart("User[profilePicture]", new FileBody(userPhotoId));
the binary file/photo is sent properly. but i need to set mimeType for the entity due to security needed on the web application to check the incoming files to prevent binary malware. so can anyone tell me why file is not sent when i use mimeType in the entity request?
p.s. I have imported httpclient-4.3.2 , httpmime-4.3.2 , httpcore-4.3.1 in my project's libs and compiling with SDK 19.
OK i just figured it out kinda; wrote the whole thing in MultipartEntityBuilder; so the line regarding sending binary photo would be as:
signupEntity.addBinaryBody("User[profilePicture]", userPhotoId, ContentType.create("image/jpeg"), ".profile.jpg");