//上传图片点击
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所得