我正在使用koush/ion将图片上传到服务器。图片上传很好,但进度条在完成后没有消失。我一直在检查代码,一切似乎都没问题。我尝试了.progressBar
的{{1}}参数以及使用progressBar.setVisibility参数手动执行此操作。这是我的代码:
Ion
布局/ row_chat.xml
public class ChatRowsAdapter extends ArrayAdapter<ChatMessage> {
private TextView messageBody;
private ImageView messagePic;
private ProgressBar progressBar;
private List<ChatMessage> countries = new ArrayList<ChatMessage>();
private LinearLayout wrapper;
private static final String TAG = "ChatRows";
private boolean loading = false;
@Override
public void add(ChatMessage object) {
countries.add(object);
super.add(object);
}
public ChatRowsAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount() {
return this.countries.size();
}
public ChatMessage getItem(int index) {
return this.countries.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_chat, parent, false);
//}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
ChatMessage coment = getItem(position);
messageBody = (TextView) row.findViewById(R.id.comment);
messagePic = (ImageView) row.findViewById(R.id.imgPhotoSendVC);
progressBar = (ProgressBar) row.findViewById(R.id.progressBarEnvioFoto);
Log.d(TAG, "Picture path: " + coment.info.getPath());
Log.d(TAG, "Text: " + coment.comment);
if (coment.info != null && coment.info.getPath() != "") {
messagePic.setVisibility(View.VISIBLE);
messageBody.setVisibility(View.GONE);
Picasso.with(parent.getContext()).load(new File(coment.info.getPath())).resize(120, 120).
into(messagePic);
if (!loading){
uploadImage(parent.getContext(), coment.info);
}
//else {
// progressBar.setVisibility(View.VISIBLE);
//}
} else {
messagePic.setVisibility(View.GONE);
messageBody.setVisibility(View.VISIBLE);
Log.d(TAG, "Setting text..");
messageBody.setText(coment.comment);
}
WindowManager temp = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = temp.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int) (size.x * 0.75);
messageBody.setMaxWidth(width);
if (coment.left) {
messageBody.setTextColor(Color.rgb(104, 159, 56));
messageBody.setBackgroundResource(R.drawable.white_bubble);
} else {
messageBody.setTextColor(Color.rgb(255, 255, 255));
messageBody.setBackgroundResource(R.drawable.green_bubble);
}
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
public void uploadImage(Context context, EnvioFotoInfo info) {
Log.d(TAG, "Loading picture..");
loading = true;
Ion.with(context)
.load("POST", "https://portaldev.mediconecta.com/api/v1/WebMethods/fotochat")
.addHeader("authorization", "Basic c2Z1c2VyZGV2Ojg2NDJFRjkyM0U1OTYxQjg3MzFDRjI0QTBCOTkzMQ==")
.progressBar(progressBar)
.setMultipartParameter("idQuote", info.getCitaId())
.setMultipartFile("file", new File(info.getPath()))
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (result != null) {
Log.d(TAG, result.toString());
} else {
Log.e(TAG, "No result");
}
loading = false;
//progressBar.setVisibility(View.GONE);
}
});
}
}