//上传头像
private void initChange() {
View view = View.inflate(getActivity(),R.layout.pull_image_recycleriew,null);
camera = view.findViewById(R.id.camera);
pick = view.findViewById(R.id.pick);
cancel = view.findViewById(R.id.cancel);
mPop = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
//设置焦点
mPop.setFocusable(true);
//设置是否可以触摸
mPop.setTouchable(true);
mPop.setBackgroundDrawable(new BitmapDrawable());
mPop.dismiss();
mPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
changeWindowAlfa(1f);//pop消失,透明度恢复
}
});
//点击取消
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPop.dismiss();
}
});
mPop.setOutsideTouchable(true);
//点击展示
mine_image_head.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//改变窗口透明度
changeWindowAlfa(0.6f);//改变窗口透明度
mPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
mPop.update();
mPop.showAsDropDown(v, Gravity.BOTTOM,0 ,0 );
initAddress();
}
});
}
void changeWindowAlfa(float alfa){
WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
params.alpha = alfa;
getActivity().getWindow().setAttributes(params);
}
//设置图片
private void initAddress() {
//点击相机
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(intent, 1000);
}
});
//打开相册
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1000);
}
});
}
//回调设置图片
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == RESULT_OK) {
Uri uri = data.getData();
Intent cIntent = new Intent("com.android.camera.action.CROP");
cIntent.setDataAndType(uri, "image/*");
cIntent.putExtra("crop", true);
cIntent.putExtra("aspactX", 1);
cIntent.putExtra("aspactY", 1);
cIntent.putExtra("outputX", 70);
cIntent.putExtra("outputY", 70);
cIntent.putExtra("return-data", true);
startActivityForResult(cIntent, 2000);
}
if (requestCode == 2000 && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
String crop = saveImage("crop", bitmap);
Map<String, String> map = new HashMap<>();
Map<String, RequestBody> mapBody = new HashMap<>();
File file=new File(crop);
if(file.exists()){
RequestBody requestBody = RxpartMapUtils.toRequestBodyOfImage(file);
mapBody.put("image"+"\";filename=\""+file.getName(), requestBody);
}
iPresenter.pBodyRequest(Apis.URL_PUT_HEADPIC_POST, map, mapBody, MyPullHandPicBean.class);
}
}
//保存图片
private String saveImage(String crop, Bitmap bitmap) {
File appDir = new File(Environment.getExternalStorageDirectory().getPath());
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = crop + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
工具类``
package com.zjh.administrat.torchbearer_power.bag.utilsbag;
import java.io.File;
import okhttp3.MediaType;
import okhttp3.RequestBody;
public class RxpartMapUtils {
public static RequestBody toRequestBodyOfImage(File file) {
RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);
return fileBody;
}
}
//处理图片
@Multipart
@POST
rx.Observable<ResponseBody> postFormBody(@Url String urlStr,@QueryMap Map<String, String> map, @PartMap Map<String, RequestBody> requestBodyMap);
/**
* 表单post请求****************************
*/
public void postFormBody(String url, Map<String, String> map, Map<String, RequestBody> mapBody, HttpListener listener) {
if (map == null) {
map = new HashMap<>();
}
if (mapBody == null){
mapBody = new HashMap<>();
}
baseApi.postFormBody(url, map, mapBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(listener));
}