一次性上传多张图片,并在上传的过程中可以继续添加、删除、点击重试等,显示失败、成功、加阴影蒙层等,注意图片对象的状态属性如:
private boolean isUploadFinish;//是否上传完成
private boolean isUploading;//是否正在上传
private boolean isUploaded;//是否被上传过
private int progress;//上传百分比
private boolean isCancelled;//是否取消上传
private boolean isFailed;//是否上传失败
private long uploadingId = -1 ;//上传成功返回的ID
private boolean selected;//是否被选中
通过不同属性状态来控制图片上传
//上传单张图片
private void uploadPhoto(final ArrayList<Photo> selectPhotoList, final int photosId, final PhotoListAdapter adapter, final int flag, final int position) {
final Map<String, String> params = new HashMap<>();
new Thread(new Runnable() {
@Override
public void run() {
final Photo photo = selectPhotoList.get(position);
String path = selectPhotoList.get(position).getPhotoUrl();
String pictureBase64 = "";
if (NetworkUtil.isNetworkAvailable()) {//只在有网络的情况下才转为Base64字符串
try {
pictureBase64 = Base64.encodeToString(FileUtil.toByteArray(new File(path)), Base64.NO_WRAP);
} catch (IOException e) {
e.printStackTrace();
}
}
String pic_suffix = path.substring(path.lastIndexOf(".") + 1, path.length());
// 开始上传
params.put("id", ((MainActivity) activity).id);
params.put("name", pic_suffix);
params.put("content", pictureBase64);
if (selectPhotoList.size() <= position) {
return;
}
selectPhotoList.get(position).setUploaded(true);
adapter.setData(selectPhotoList);
OkHttpUtil.postContent(url, params, path, new ResultCallback<String>() {
@Override
public void onError(Request request, Exception e, int id) {
if (selectPhotoList.contains(photo)) {
int position = selectPhotoList.indexOf(photo);
selectPhotoList.get(position).setFailed(true);
selectPhotoList.get(position).setUploading(false);
adapter.setData(selectPhotoList);
adapter.notifyItemChanged(position);
}
//发消息给下一个上传任务 mHandler.sendEmptyMessage(photosId);
}
@Override
public void onResponse(String responseString, int id) {
BaseResponseObject responseObject = WebFrontUtil.getResponseObject(responseString);
String message = responseObject.getResponseMessage();
boolean status = responseObject.getResponseStatus();
if (status) {
if (selectPhotoList.contains(photo)) {
int position = selectPhotoList.indexOf(photo);
//获取图片id
Long photoId = responseObject.getPhotoId();
selectPhotoList.get(position).setUploadingId(photoId);
//进度条消失
selectPhotoList.get(position).setUploadFinish(true);
selectPhotoList.get(position).setUploading(false);
selectPhotoList.get(position).setFailed(false);
adapter.setData(selectPhotoList);
adapter.notifyItemChanged(position);
}
//发消息给下一个上传任务
mHandler.sendEmptyMessage(photosId);
if (selectPhotoList.contains(photo)) {
int position = selectPhotoList.indexOf(photo);
selectPhotoList.get(position).setFailed(true);
selectPhotoList.get(position).setUploading(false);
adapter.setData(selectPhotoList);
adapter.notifyItemChanged(position);
}
//发消息给下一个上传任务 mHandler.sendEmptyMessage(photosId);
}
}
@Override
public void inProgress(float progress, int id) {
super.inProgress(progress, id);
int pro = (int) (100 * progress);
if (selectPhotoList.contains(photo)) {
int position = selectPhotoList.indexOf(photo);
selectPhotoList.get(position).setProgress(pro);
adapter.setData(selectPhotoList);
adapter.notifyItemChanged(position);
}
}
});
}
}).start();
}