我的应用允许用户从图库中选择图像并将其上传到服务器。目前我可以显示图像但不上传。我没有收到错误。以下是我的代码,我希望很清楚。
public void submit_click(View view) {
TextView err=(TextView) findViewById(R.id.err);
if (thumbnail != null) {
// TextView err = (TextView) findViewById(R.id.err);
try{
byte[] bitmapdata = imagetoArray();
img = decodeUTF8(bitmapdata);
if (updateImage() == true) {
Intent myIntent = new Intent(view.getContext(), ProfileActivity.class);
startActivityForResult(myIntent, 0);
} else {
err.setText("Error update image");
}
}
catch(Exception ex)
{
err.setText(ex.getLocalizedMessage());
}
}
}
private boolean updateImage() {
boolean status = false;
TextView err=(TextView) findViewById(R.id.err);
String username = SessionManager.getMaps("user");
String postData = "{\"UserImage\":\"" + img + "\",\"UserName\":\"" + username + "\"}";
try {
String domain = getString(R.string.domain);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httppost = new HttpPost(domain + "updateUserImage");
StringEntity se = new StringEntity(postData.toString(), "utf-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
response = httpclient.execute(httppost);
if (response != null) {
HttpEntity r_entity = response.getEntity();
String json = EntityUtils.toString(r_entity);
status = Boolean.parseBoolean(json);
}
} catch (Exception e) {
err.setText(e.getLocalizedMessage());
}
return status;
}
private byte[] imagetoArray() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
TextView err=(TextView) findViewById(R.id.err);
try{
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
}
catch(Exception ex)
{
err.setText(ex.getLocalizedMessage());
}
return stream.toByteArray();
}
private String decodeUTF8(byte[] bytes) {
String sw="";
TextView err=(TextView) findViewById(R.id.err);
try{
sw= Base64.encodeToString(bytes, Base64.NO_WRAP);
}
catch(Exception ex)
{
err.setText(ex.getLocalizedMessage());
}
return sw;
}
答案 0 :(得分:1)
如果你想处理一个click事件,最好在按钮上添加一个事件处理程序。
submit.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onClick(View v) {
submit_click(v);
}
});
PS:对你的程序员有一颗心,并阅读:Java Code Conventions