protected static final int CHOOSE_PICTURE = 0;
protected static final int TAKE_PICTURE = 1;
private static final int CROP_SMALL_PICTURE = 2;
protected static Uri tempUri;
private LoginModel loginModel;
//点击事件
public void onClick(View view) {
switch (view.getId()){
case R.id.iv_toug:
showChoosePicDialog();
break;
}
}
//跳转到相册,相机
protected void showChoosePicDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("设置头像");
String[] items = { "选择本地照片", "拍照" };
builder.setNegativeButton("取消", null);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case CHOOSE_PICTURE: // 选择本地照片
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,CHOOSE_PICTURE);
break;
case TAKE_PICTURE: // 拍照
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
tempUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "image.jpg"));
// 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
break;
}
}
});
builder.create().show();
}
//回调借口
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // 如果返回码是可以用的
switch (requestCode) {
case TAKE_PICTURE:
startPhotoZoom(tempUri); // 开始对图片进行裁剪处理
break;
case CHOOSE_PICTURE:
startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理
break;
case CROP_SMALL_PICTURE:
if (data != null) {
setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
}
break;
}
}
}
/**
* 保存裁剪之后的图片数据
*
* @param
*
*/
protected void setImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
// photo = Utils.toRoundBitmap(photo, tempUri); // 这个时候的图片已经被处理成圆形的了
cvtou.setImageBitmap(photo);
//保存操作
saveBitmap(photo);
}
}
//保存操作
private Uri saveBitmap(Bitmap bm){
File tmDir=new File(Environment.getExternalStorageDirectory()+"/avaterr");
if(tmDir.exists()){
tmDir.mkdir();
}
File img=new File(tmDir.getAbsolutePath()+"avater.png");
try {
FileOutputStream fos = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.PNG,85,fos);
loginModel=new LoginModel(XqActivity.this);
loginModel.touimg("97",img);
fos.flush();
fos.close();
return Uri.fromFile(img);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//剪切操作
protected void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
tempUri = uri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
//-----------------------------Moudle层中的头像上传请求----------------------------
public void touimg(String mobile, File file){
Map<String,Object> params=new HashMap<>();
params.put("uid",mobile);
OkHttpClient okHttpClient=new OkHttpClient();
MultipartBody.Builder builder=new MultipartBody.Builder().setType(MultipartBody.FORM);
RequestBody requestBody=RequestBody.create(MediaType.parse("image/*"),file);
builder.addFormDataPart("file",file.getName(),requestBody);
if(params!=null){
for (Map.Entry entry : params.entrySet()) {
builder.addFormDataPart(valueOf(entry.getKey()), valueOf(entry.getValue()));
}
Request request=new Request.Builder().url(Api.IMG_API).post(builder.build()).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("=======失败了===");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("=======成功了===");
}
});
}
}