Android 图库图像转为 Base 64 字符串
我的应用程序允许用户从图库中选择图像并将其上传到服务器。目前我可以显示图像但不能上传它。我没有收到错误。下面是我的代码,我希望它是清楚的。
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;
}
My app allows a user to select an image from the gallery and upload it to a server. Currently I can display the image but not upload it. I am not getting an error. Below is my code, I hope that it is clear.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想处理单击事件,最好向按钮添加事件处理程序。
PS:对你的程序员同事有一颗爱心,请阅读:Java 代码约定
If you want to handle a click event it would be a good idea to add a event handler to the button.
PS: Have a heart for your fellow programmer and read that: Java Code Conventions