private String uploadFile() {
String responseString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, bos);
bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, true);
byte[] data = bos.toByteArray();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Apis.Base);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
ByteArrayBody bab = new ByteArrayBody(data, "bt.jpg");
entity.addPart("gallery", bab);
entity.addPart("action", new StringBody("update_photos"));
entity.addPart("user_id", new StringBody(SaveSharedPreference.getUserID(GalleryFragment.this)));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
statusCode = response.getStatusLine().getStatusCode();
responseString = EntityUtils.toString(r_entity);
jsonObject = new JSONObject(responseString);
Log.e("respon", "" + responseString);
if (statusCode == 200) {
// Server response
SaveSharedPreference.setUserIMAGE(GalleryFragment.this, "https://pickpamphlet.com/PropertyApp/uploads/profile_pic/" + jsonObject.getJSONObject("data").getString("profile_pic"));
} else if (statusCode == 201) {
// Server response
SaveSharedPreference.setUserIMAGE(GalleryFragment.this, "https://pickpamphlet.com/PropertyApp/uploads/profile_pic/" + jsonObject.getJSONObject("data").getString("profile_pic"));
} else {
responseString = "Error occurred! Http Status Code: " +
EntityUtils.toString(r_entity) + statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (Exception e) {
responseString = e.toString();
}
return responseString;
}
制作一个类似这样的函数将图像上传到服务器并调整其大小。