首先添加权限
代码
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.icon://点击修改头像
//弹出Dialog
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View view=View.inflate(getActivity(),R.layout.alert,null);
mAlertDialog = mBuilder.create();
//添加布局
mAlertDialog.setView(view);
//展示弹框
mAlertDialog.show();
TextView camera=view.findViewById(R.id.camera);
TextView picture=view.findViewById(R.id.picture);
//拍照
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/xx.png"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(intent,100);
}
});
//从图库选择
picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,200);
}
});
break;
}
}
回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//调用系统相机
if(requestCode==100&&resultCode==getActivity().RESULT_OK){
crop(mUri);
}
//从图库选择
if(requestCode==200&&resultCode==getActivity().RESULT_OK){
crop(data.getData());
}
if(requestCode==300&&resultCode==getActivity().RESULT_OK){
Bitmap bitmap=data.getParcelableExtra("data");
icon.setImageBitmap(bitmap);
//弹框消失
mAlertDialog.dismiss();
//保存图片到sd卡
String path=saveImage("crop", bitmap);
File file=new File(path);
//请求上传图片操作
mMyPresenter.upLoadIcon(uid,file);
}
}
保存图片的方法
//保存图片
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.PNG, 100, fos);
fos.flush();
fos.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
裁剪方法
private void crop(Uri uri) {
Intent intent=new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
intent.putExtra("crop",true);
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
intent.putExtra("outputX",250);
intent.putExtra("outputY",250);
intent.putExtra("return-data",true);
startActivityForResult(intent,300);
}
上传图片的api
//上传头像
@Multipart
@POST("file/upload")
Observable uploadicon(@Query("uid") String uid, @Part MultipartBody.Part file);
裁剪完图片设置控件处请求上传头像数据
//请求上传图片操作
mMyPresenter.upLoadIcon(uid,file);
Presenter
//上传头像
public void upLoadIcon(String uid, File file) {
mMyModel.upLoadIcon(uid,file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(Object o) {
if (o != null) {
if (mIView != null)
mIView.success(o,2);
return;
}
if (mIView != null)
mIView.failed(new Throwable("上传头像失败"));
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
mIView.failed(new Throwable("上传失败"));
}
});
}
Model
public Observable upLoadIcon(String uid, File file) {
MyApi myApi = RetrofitUtil_Home.getInstance().create(MyApi.class);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"),file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file",file.getName(),requestBody);
Observable icon = myApi.uploadicon(uid, part);
return icon;
}
具体也可以参考
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/1102/8678.html