多图片的上传

一次性上传多张图片,并在上传的过程中可以继续添加、删除、点击重试等,显示失败、成功、加阴影蒙层等,注意图片对象的状态属性如:

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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值