Commit 98845562 by lihuaikun

fix:增加图片上传压缩 加长http连接时间

parent 5ce668d4
......@@ -45,6 +45,6 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation project(":baselib")
implementation 'top.zibin:Luban:1.1.8'
annotationProcessor deps.butterknife.butterknife_compiler
}
\ No newline at end of file
......@@ -59,6 +59,9 @@ import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;
public class PersonFragment extends BaseFragment implements View.OnClickListener {
......@@ -324,45 +327,76 @@ public class PersonFragment extends BaseFragment implements View.OnClickListener
*/
private void updateAvatar(String path) {
if (!TextUtils.isEmpty(path)) {
if (!TextUtils.isEmpty(path)&&new File(path).exists()) {
HashMap<String,RequestBody> map=new HashMap<>();
Luban.with(getContext())
.load(path)
.ignoreBy(100)
.setTargetDir(new File(path).getParentFile().getPath())
.filter(new CompressionPredicate() {
@Override
public boolean apply(String path) {
return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
}
})
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
// TODO 压缩开始前调用,可以在方法内启动 loading UI
showLoadingDialog();
}
RequestBody updateTypeBody=RequestBody.create(MediaType.parse("application/json"),"avatar");
map.put("update_type",updateTypeBody);
@Override
public void onSuccess(File file) {
// TODO 压缩成功后调用,返回压缩后的图片文件
File file = new File(path);
HashMap<String,RequestBody> map=new HashMap<>();
RequestBody fileRQ = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), fileRQ);
RequestBody updateTypeBody=RequestBody.create(MediaType.parse("application/json"),"avatar");
map.put("update_type",updateTypeBody);
RequestBody fileRQ = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), fileRQ);
// map.put("file",fileRQ);
UserServiceFactory.updateInfo(map,part).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel<UserModel>>() {
@Override
public void onSubscribe(Disposable d) {
showLoadingDialog();
}
UserServiceFactory.updateInfo(map,part).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel<UserModel>>() {
@Override
public void onSubscribe(Disposable d) {
@Override
public void onNext(BaseModel<UserModel> stringBaseModel) {
if (stringBaseModel.getData()!=null){
String avatar = stringBaseModel.getData().getAvatar();
UserModel userModel = DKUserManager.getUserInfo();
}
userModel.setAvatar(avatar);
DKUserManager.updateUserInfo(userModel);
GlideUtils.loadCircleImage(getContext(), avatar, ivAvatar);
}
@Override
public void onNext(BaseModel<UserModel> stringBaseModel) {
if (stringBaseModel.getData()!=null){
String avatar = stringBaseModel.getData().getAvatar();
UserModel userModel = DKUserManager.getUserInfo();
userModel.setAvatar(avatar);
DKUserManager.updateUserInfo(userModel);
GlideUtils.loadCircleImage(getContext(), avatar, ivAvatar);
}
}
@Override
public void onComplete() {
super.onComplete();
dismissLoadingDialog();
}
});
}
@Override
public void onError(Throwable e) {
// TODO 当压缩过程出现问题时调用
Logger.e(e.getMessage());
ToastUtils.showLong("图片压缩失败");
dismissLoadingDialog();
}
}).launch();
}
@Override
public void onComplete() {
super.onComplete();
dismissLoadingDialog();
}
});
}
}
......
......@@ -29,6 +29,7 @@ public class BaseApi {
: "http://tp.lzdu.com/api/";
public static final String OTHER_BASE_URL="http://gd21262648.imwork.net:30110/";
public static final int TIME_OUT=60;
private static LinkedHashMap<String, Retrofit> linkedHashMap = new LinkedHashMap<>(3);
......@@ -80,8 +81,9 @@ public class BaseApi {
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient
okHttpClient = new OkHttpClient.Builder().readTimeout(7000, TimeUnit.MILLISECONDS)
.connectTimeout(7000, TimeUnit.MILLISECONDS)
okHttpClient = new OkHttpClient.Builder().readTimeout(TIME_OUT, TimeUnit.SECONDS)
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
.writeTimeout(TIME_OUT, TimeUnit.SECONDS)
.addInterceptor(new TokenInterceptor())
.addInterceptor(httpLoggingInterceptor)
.hostnameVerifier(new HostnameVerifier() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment