很遗憾,我没有成功地使用Venky的代码。经过两天的努力,我终于找到了解决方案,并希望它能帮助那些有同样问题的人。请随意修改代码,因为这个代码是基于我的结构的。
注意:bitmapProfileImage
是全局变量,保存位图图像(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, "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;
}
private static boolean isStatusOk(int statusCode) {
if((statusCode >= HttpURLConnection.HTTP_OK) && (statusCode <= HttpURLConnection.HTTP_PARTIAL))
return true;
return false;
}
}
因此,在您的活动/片段中,您应该有AsyncTask
类。在doInBackground
方法中添加以下代码:
@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;
}
return -1;
}
最后,为了使用
MultipartEntity
,您需要下载4个jar文件。其中一个是
apache-mime4j,其余的是
httpclient、httpcore和httpmime。您应该提取jar文件并将其复制/粘贴到项目的
./libs
文件夹中。
另外还有一个
有用的示例。