1,弹出dialog 相机按钮,相册按钮,
//弹出dialog
private void getDialog() {
dialog = new Dialog(this);
//填充对话框的布局
View inflate = LayoutInflater.from(this).inflate(R.layout.popup_window_button, null);
//初始化控件
inflate.findViewById(R.id.takePhoto).setOnClickListener(this);
inflate.findViewById(R.id.choosePhoto).setOnClickListener(this);
inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);
//将布局设置给Dialog
dialog.setContentView(inflate);
//获取当前Activity所在的窗体
Window dialogWindow = dialog.getWindow();
if (dialogWindow == null) {
return;
}
//设置Dialog从窗体底部弹出
dialogWindow.setGravity(Gravity.BOTTOM);
//获得窗体的属性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.y = 10;//设置Dialog距离底部的距离
//将属性设置给窗体
dialogWindow.setAttributes(lp);
dialog.show();//显示对话框
}
2,跳转相机,相册的点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.takePhoto:
dialog.dismiss();
File f = new File(imgRoot, new Date().getTime() + ".jpg");
uri = Uri.fromFile(f);
// ACTION_IMAGE_CAPTURE(动作图像捕获)
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 拍照输出的路径 (EXTRA_OUTPUT额外输出)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, FLAG_CAMERA_REQUEST);
break;
case R.id.choosePhoto:
dialog.dismiss();
//case R.id.btn_choose_album:
intent = new Intent(Intent.ACTION_PICK);
// 设置类型 image/jpeg image/png image/gif,*是通配符
intent.setType("image/*");
startActivityForResult(intent, FFLAG_ALBUM_REQUEST);
break;
在ActivityResult方法里 获取到uri,转换为file文件
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(uri, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
Uri uri2 = new Uri.Builder()
.scheme(UriUtil.LOCAL_FILE_SCHEME)
.path(imagePath)
.build();
instanll_img_logo.setImageURI(uri2);
//选择是否存入sp ,
SharedPreferences sp = getContext().getSharedPreferences("user", 0);
SharedPreferences.Editor edit = sp.edit();
edit.putString("icon", String.valueOf(uri2));
edit.commit();
c.close();
file = new File(imagePath);
loadImg(file);
//调用P层方法,传值
presenter.upLoad(uid, file);
M层
public Observable upLoad(int uid, File file) {
ILoginBean iLoginBean = RetrofitManager.getInstence().create(ILoginBean.class);
//RequestBody封装了文件和文件的类型
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
// MultipartBody.Part封装了接受的key和文件名字和RequestBody
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
return iLoginBean.upLoad(uid, part);
}
P层
@SuppressLint("CheckResult")
public void upLoad(int uid, File file) {
model.upLoad(uid,file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(UpLoadBean upLoadBean) throws Exception {
if (iView != null){
if (upLoadBean != null& "0".equals(upLoadBean.getCode())){
iView.onSuccess(upLoadBean);
}
}
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) throws Exception {
if (iView != null){
iView.onFailed(throwable);
}
}
});
p层调用view层返回结果的方法。 转换结束。