Unfortunately I was not successful to use Venky's code. After two day finally I found my solution and hope it helps those that have same problem. Feel free to modify the code since this code is based on my structure.
Note: bitmapProfileImage
is global variable that holds bitmap image (Bitmap bitmapProfileImage;
)
public class ContentDownloader {
public static String getHttpPutContent(String url, String token, MultipartEntity multipartEntity) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
put.setHeader("token", token);
// Log.i(TAG, "Token=> " + token);
Log.i(TAG, "Try to open=> " + url);
MultipartEntity reqEntity = multipartEntity;
put.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(put);
int statusCode = httpResponse.getStatusLine().getStatusCode();
Log.i(TAG, "Connection code: " + statusCode);
HttpEntity entity = httpResponse.getEntity();
String serverResponse = EntityUtils.toString(entity);
Log.i(TAG, "Server response=> " + serverResponse);
if(!isStatusOk(statusCode))
return null;
return serverResponse;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param statusCode
* @return True if connection code is 2xx, False otherwise.
*/
private static boolean isStatusOk(int statusCode) {
if((statusCode >= HttpURLConnection.HTTP_OK) && (statusCode <= HttpURLConnection.HTTP_PARTIAL))
return true;
return false;
}
}
So in your activity/fragment you should have AsyncTask
class. Add following code inside doInBackground
method:
@Override
protected Integer doInBackground(Void... params) {
String content = "";
String url = LinkManager.getUserProfileUrl();
Person person = SpStorage.getPersonInfo(context);
String imgPath = SpStorage.getProfilePicPath(context);
String imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1);
if(bitmapProfileImage == null) {
Log.i(TAG, "bitmap is null. Try to get it from profile pic directory...");
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + ImageDownloader.DIRECTORY_NAME + File.separator + PROFILE_PICS;
imgPath = new File(file_path, imgName).getAbsolutePath();
bitmapProfileImage = BitmapFactory.decodeFile(imgPath);
}
if(bitmapProfileImage != null) {
Log.i(TAG, "Image address: " + imgPath);
try {
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("first_name", new StringBody(person.getName()));
reqEntity.addPart("last_name", new StringBody(""));
reqEntity.addPart("email",new StringBody(person.getEmail()));
reqEntity.addPart("birthday", new StringBody(person.getBirthDay()));
reqEntity.addPart("gender", new StringBody(person.getGender(person.getGender())));
reqEntity.addPart("orientation", new StringBody(person.getOrientation(person.getGender(), person.getLookingFor())));
reqEntity.addPart("relationship_status", new StringBody(person.getStatus(person.getStatus())));
reqEntity.addPart("city", new StringBody(person.getCity()));
reqEntity.addPart("about", new StringBody(person.getAboutMe()));
reqEntity.addPart("device_type", new StringBody("Android"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmapProfileImage.compress(Bitmap.CompressFormat.PNG, 0, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "profilePic.png");
reqEntity.addPart("photo", bab);
content = ContentDownloader.getHttpPutContent(url, SpStorage.getToken(context), reqEntity);
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
} else
Log.e(TAG, "Image not found :(");
if(!TextUtils.isEmpty(content)) {
return 0; // registration successful
}
return -1;
}
Finally, in order to use MultipartEntity
, You are needed to download 4 jar files. One is apache-mime4j and the rest are httpclient, httpcore and httpmime. You should extract jar files and copy/paste it int ./libs
folder of your project.
Another use full sample