一、只传一张图片
1.依赖包
compile 'top.zibin:Luban:1.1.3'
2.
@POST("地址")
Observable<UpLoadAvatarBean> uploaduseravatar(@Body RequestBody avatar);
3.p层
// 上传用户头像
public void uploadavatar(RequestBody avatar) {
Api.getServer().uploaduseravatar(avatar)
.compose(schedulersTransformer2())
.subscribe(new HttpObserver<UpLoadAvatarBean>() {
@Override
protected void onSuccess(UpLoadAvatarBean upLoadAvatarBean) {
mView.UploadAvatarData(upLoadAvatarBean);
}
@Override
protected void onFail(Throwable e) {
mView.onFail("" + e);
}
});
}
4.activity层
1)方法:
//鲁班压缩
private void luBanCompressImage(File compressFile) {
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
String filename = timeStampFormat.format(new Date());
Luban
.with(this)
.load(compressFile)
.ignoreBy(100)
.setTargetDir(Environment.getExternalStorageDirectory().toString())
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
//开始压缩了 加一个loading加载框
Log.e(TAG, "luban压缩 onStart: 开始压缩了");
}
@Override
public void onSuccess(File file) {
RequestBody requestFile = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("上传图片照片名字", file.getName(),
RequestBody.create(MediaType.parse("image/*"),
file))
.addFormDataPart("参数", 参数)
.addFormDataPart("参数", 需要的参数)
.addFormDataPart("参数", 需要的参数)
.build();
mPresenter.uploadavatar(requestFile);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "luban压缩: 失败了" + e.getMessage());
}
}).launch();
}
2)在图片要赋到控件的那里写上
luBanCompressImage(new File("string类型的图片路径"));
5.v层就不写了