ImageSelector框架使用,并用OkHttp框架上传到服务器

//上传图片点击
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置点击图片click为1
                click = 1;

                //使用ImageSelector框架打开相册
                ImageSelector.builder()
                        .useCamera(true) // 设置是否使用拍照
                        .setSingle(false)  //设置是否单选
                        .setMaxSelectCount(9) // 图片的最大选择数量,小于等于0时,不限数量。
                        .start(editActivity.this, REQUEST_CODE); // 打开相册


            }
        });


 //回调相册方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && data != null) {
            //获取图片所有路径
            ArrayList<String> images = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
            System.out.println("dasdsdsd" + images.toString());
            //拼接路径方便存储
            stringBuffer = new StringBuffer();
            //循环发送到服务器
            for (int i = 0; i < images.size(); i++) {
                File file = new File(images.get(i));


                filename = "attention_photo" + System.currentTimeMillis()+".jpg";
                stringBuffer.append(filename+";");

                RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("upload", filename, RequestBody.create(MediaType.parse("multipart/form-data"), file))
                        .build();
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .header("Authorization","Client-ID"+UUID.randomUUID())
                        .url("http://192.168.43.243:9000/upload")
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        String err = e.getMessage().toString();
                        System.out.println("---------------------------sbsbsbs+err"+err);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String s = response.body().string();
                        System.out.println("---------------------------sbsbsbs+success"+s);
                    }
                });




            }

            System.out.println("---------------------------sbsbsbs+stringBuffer"+stringBuffer.toString());




        }
    }

观https://blog.csdn.net/qq_40543575/article/details/79832051所得

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值