我正在开发一个Android应用程序,我需要将图像上传到服务器。缩放位图并上传到服务器,UI线程阻塞
之前我上传的图片,我就扩展到500与Bitmap.createScaledBitmap()
一个最大宽度/高度我的问题是,即使我做这两个步骤的UI线程获取的卡在后台线程中。
我的代码如下所示:
dialog = ProgressDialog.show(this, "", "Loading...", true);
// upload the image to the server
Thread t = new Thread() {
@Override
public void run() {
bmp = BitmapHelper.scaleBmp(bmp, 500);//bmp is a private class Bitmap
try {
HttpResponse response = Helper.uploadBitmap(bmp);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
finalResult = new JSONObject(builder.toString());
} catch (Exception e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
handleData();
}
});
}
};
t.run();
关于如何正确做到这一点任何帮助将非常感激!
编辑:添加handleData()函数
public void handleData() {
take.setVisibility(View.VISIBLE);
select.setVisibility(View.VISIBLE);
process.setVisibility(View.GONE);
select_new.setVisibility(View.GONE);
message = "An error occured! Please try again.";
boolean success = false;
try {
success = finalResult.getBoolean("success");
url = "http://url";
url += finalResult.getString("path");
thumbnail = finalResult.getString("thumbnail");
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
if (success) {
message = "Picture was uploaded successfuly!";
}
dialog.dismiss();
Config.toast(this, message);
Thread t = new Thread() {
@Override
public void run() {
final Bitmap b = HttpHelper.getBitmapFromURL(thumbnail);
preview_image.post(new Runnable() {
@Override
public void run() {
preview_image.setImageBitmap(b);
preview_image.setVisibility(View.VISIBLE);
preview_image.setOnClickListener(context);
}
});
}
};
t.run();
}
handleData是做什么的?它在UI上做了什么? – brianestey
刚刚添加功能的帖子。问题是,即使在显示对话框之前它仍然卡住(第一段代码) – Olivier