case R.id.infoSimp://点击头像弹窗
mBottomSheetDialogp = new BottomSheetDialog(UserinfoActivity.this);
mBottomSheetDialogp.setContentView(R.layout.pic_alert);
mBottomSheetDialogp.setCancelable(true);
mBottomSheetDialogp.show();
TextView btn_alert_pai = mBottomSheetDialogp.findViewById(R.id.btn_alert_pai);
TextView btn_alert_ce = mBottomSheetDialogp.findViewById(R.id.btn_alert_ce);
TextView btn_alert_qu = mBottomSheetDialogp.findViewById(R.id.btn_alert_qu);
btn_alert_qu.setOnClickListener(this);
btn_alert_ce.setOnClickListener(this);
btn_alert_pai.setOnClickListener(this);
break;
case R.id.btn_alert_ce://相册上传
mBottomSheetDialogp.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 200);
break;
case R.id.btn_alert_pai://拍照上传
mBottomSheetDialogp.dismiss();
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(img)));
startActivityForResult(intent2, 100);
break;
//上传头像
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
mUri = data.getData();
String imagePath = Utils.getImagePath(mUri, UserinfoActivity.this);
doPic(0, Api.picUrl, mapH, null, imagePath);
}
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
mUri = Uri.fromFile(new File(img));
if (mUri != null) {
doPic(0, Api.picUrl, mapH, null, img);
}
}
}
//添加注解
@Multipart
@POST
Observable<ResponseBody> doPic(@Url String url, @HeaderMap Map<String, String> mapH, @QueryMap Map<String, String> mapQ, @Part MultipartBody.Part part);
//设置拦截器
public OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
})
.cache(new Cache(new File(Environment.getExternalStorageDirectory().getAbsolutePath()), 10 * 1024 * 1024))
.build();
return client;
}
//封装网络判断工具
public static boolean isNetWork(Context context) {
if (context != null) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
return info.isAvailable();
}
}
return false;
}
//封装上传头像请求方法
public HttpUtis doPic(String url, Map<String, String> mapH, Map<String, String> mapQ, String path) {
if (mapH == null) {
mapH = new HashMap<>();
}
if (mapQ == null) {
mapQ = new HashMap<>();
}
//处理图片路径
MediaType type = MediaType.parse("multipart/form-data;charset=utf-8");
File file = new File(path);
RequestBody body = RequestBody.create(type, file);
MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), body);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(getClient())
.build();
BaseService baseService = retrofit.create(BaseService.class);
Observable<ResponseBody> ob = baseService.doPic(url, mapH, mapQ, part);
//抽取共同的方法
doHttp(ob);
return this;
}
private void doHttp(Observable<ResponseBody> ob) {
ob.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new BlockingBaseObserver<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
try {
listener.success(responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
listener.error(e.getMessage());
}
});
}
private HttpListener listener;
public void getHttpListener(HttpListener listener) {
this.listener = listener;
}
//创建接口回调数据
public interface HttpListener {
void success(String data);
void error(String error);
}
头像上传,工具类封装
最新推荐文章于 2021-05-25 18:14:05 发布