接口地址 https://www.zhaoapi.cn/file/upload 参数 uid=用户id&file=文件 //上传头像 @Multipart @POST(AppConstant.USER_UP_LOAD_PIC) CallupLoadPic(@Query("uid") String mobile, @Part MultipartBody.Part part);
//diaolog点击显示
final Dialog mdialog = new Dialog(getActivity(), R.style.my_dialog); View dialog_view = LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.dialog_layout, null); TextView xiangji = dialog_view.findViewById(R.id.xiangji); TextView xiangce = dialog_view.findViewById(R.id.xiangce); TextView quxiao = dialog_view.findViewById(R.id.quxiao); //取消 quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mdialog.cancel(); } }); //相机 xiangji.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //打开相机 MediaStore.ACTION_IMAGE_CAPTURE 打开相机的Action Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //在Sdcard 中创建文件 存入图片 String path = Environment.getExternalStorageDirectory() + "usericon.png";
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path))); //1.意图 2.requestCode 请求码 startActivityForResult(it, 1000); mdialog.cancel(); } }); //相册 xiangce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //调取系统的相册 Intent.ACTION_PICK相册 Intent it = new Intent(Intent.ACTION_PICK); //设置格式 it.setType("image/*"); startActivityForResult(it, 3000); mdialog.cancel(); } }); mdialog.setContentView(dialog_view); mdialog.setTitle("修改用户头像"); Window window = mdialog.getWindow(); window.setGravity(Gravity.BOTTOM); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.x = 0; attributes.y = -20; attributes.width = getResources().getDisplayMetrics().widthPixels; dialog_view.measure(0, 0); attributes.height = dialog_view.getMeasuredHeight(); attributes.alpha = 9f; window.setAttributes(attributes); mdialog.setCanceledOnTouchOutside(true); mdialog.show();
//---------------------------------------------------
//activityresult 回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//当拍完照以后点击完成 会执行 onActivityResult 方法 调取裁剪功能
String s = "";
if (requestCode == 1000 && resultCode == RESULT_OK) {
//调取裁剪功能 om.android.camera.action.CROP 裁剪的Action
Intent it = new Intent("com.android.camera.action.CROP");
//得到图片设置类型
it.setDataAndType(Uri.fromFile(new File(path)), "image/*");
//是否支持裁剪 设置 true 支持 false 不支持
it.putExtra("CROP", true);
//设置比例大小 1:1
it.putExtra("aspectX", 1);
it.putExtra("aspectY", 1);
//输出的大小
it.putExtra("outputX", 250);
it.putExtra("outputY", 250);
s = "相机";
//将裁剪好的图片进行返回到Intent中
it.putExtra("return-data", true);
startActivityForResult(it, 2000);
} else if (requestCode == 3000 && resultCode == RESULT_OK) {
s = "相ce";
//得到相册图片
Uri uri = data.getData();
//裁剪
Intent it = new Intent("com.android.camera.action.CROP");
//设置图片 以及格式
it.setDataAndType(uri, "image/*");
//是否支持裁剪
it.putExtra("crop", true);
//设置比例
it.putExtra("aspectX", 1);
it.putExtra("aspectY", 1);
//设置输出的大小
it.putExtra("outputX", 250);
it.putExtra("outputY", 250);
//是否支持人脸识别
// it.putExtra("onFaceDetection", true);
//返回
it.putExtra("return-data", true);
startActivityForResult(it, 2000);
}
Log.i("xxx", "----1---" + s + "-------");
//点击完裁剪的完成以后会执行的方法
if (requestCode == 2000 && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
//userIcon.setImageBitmap(bitmap);
File resourceFile = getResourceFile(bitmap);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), resourceFile);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", resourceFile.getName(), requestBody);
SharedPreferences user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
String uid = user.getString("uid", "");
if (!uid.isEmpty() && !uid.equals("")) {
presenter.getUpLoadData(uid, part);
}
Log.i("xxx", "----2---" + s + "-------");
}
}
/-----------------------------------------------------
//生成图片file方法
private File getResourceFile(Bitmap bitmap) {
File file = new File(getActivity().getFilesDir().getAbsolutePath());
if (!file.exists()) {
//如果file不存在 则创建此文件夹
file.mkdirs();
}
//根据获取到的file文件夹 生成带根目录的图片文件
File file1 = new File(file, "icon.png");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file1);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
//刷新流
fileOutputStream.flush();
//关流
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return file1;
}