//点击图标弹出相机相册的框
llhead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MySetActivity.this, R.style.DialogTheme);
View view=View.inflate(MySetActivity.this,R.layout.myhead_dialog,null);
dialog.setContentView(view);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams attributes = window.getAttributes();
WindowManager windowManager = getWindowManager();
//获取屏幕宽度
Display defaultDisplay = windowManager.getDefaultDisplay();
attributes.width=defaultDisplay.getWidth();
window.setAttributes(attributes);
dialog.show();
//获取控件
View xiangji = view.findViewById(R.id.xiangji);
xiangji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击相机,进入到拍摄页面
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (hasSdcard()) {
// 存到sdcard中
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(path)));
//执行跳转
startActivityForResult(intent, REQUEST_CAMEAR);
dialog.dismiss();
}
}
});
//获取相册点击事件
View xiangce = view.findViewById(R.id.xiangce);
xiangce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
if (hasSdcard()) {
//设置图片格式
intent.setType("image/*");
//执行
startActivityForResult(intent, REQUEST_PICK);
dialog.dismiss();
}
}
});
View quxioa = view.findViewById(R.id.quxiao);
quxioa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
}
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_IMAGE_CAMERA && resultCode == RESULT_OK) {
Uri mPath = ((MyApplication)(MySetActivity.this.getApplicationContext())).getCaptureImage();
String file = getPath(mPath);
Bitmap bmp = MyApplication.decodeImage(file);
startRegister(bmp, file);
}
if (requestCode == REQUEST_CAMEAR && resultCode == RESULT_OK) {
//调取裁剪功能
Intent intent = new Intent("com.android.camera.action.CROP");
//将图片设置给裁剪
intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
//设置是否支持裁剪
intent.putExtra("CROP", true);
//设置宽高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置显示大小
intent.putExtra("outputX", 50);
intent.putExtra("outputY", 50);
//将图片返回给data
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_PICTRUE);
}
if (requestCode == REQUEST_PICK && resultCode == RESULT_OK) {
//打开裁剪
Intent intent = new Intent("com.android.camera.action.CROP");
Uri uri = data.getData();
//将图片设置给裁剪
intent.setDataAndType(uri, "image/*");
//设置是否可裁剪
intent.putExtra("CROP", true);
//设置宽高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置输出
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
//返回data
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_PICTRUE);
}
if (requestCode == REQUEST_CODE_IMAGE_OP && resultCode == RESULT_OK) {
Uri mPath = data.getData();
String file = getPath(mPath);
Bitmap bmp = MyApplication.decodeImage(file);
if (bmp == null || bmp.getWidth() <= 0 || bmp.getHeight() <= 0 ) {
Log.e("----", "error");
} else {
Log.i("----", "bmp [" + bmp.getWidth() + "," + bmp.getHeight());
}
startRegister(bmp, file);
} else if (requestCode == REQUEST_CODE_OP) {
Log.i("----", "RESULT =" + resultCode);
if (data == null) {
return;
}
Bundle bundle = data.getExtras();
String path = bundle.getString("imagePath");
Log.i("----", "path="+path);
}
if (requestCode == REQUEST_PICTRUE && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
if (bitmap.getByteCount()==0){
Toast.makeText(MySetActivity.this,"图片为空",Toast.LENGTH_SHORT).show();
}else {
try {
file1 = saveFile(bitmap, "/image.png");
} catch (IOException e) {
e.printStackTrace();
}
}
onFail(file1);
}
}
public void onFail(File file) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//挂载状态
if (file != null && file.exists()) {
//图片请求体
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
//对图片请求体对象,封装成multipart对象,文件表单对象
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
//P层进行网络请求
baseTwoPresenter.showFivePost(Api.USER_HEADPIC,null,filePart);
} else {
Toast.makeText(this, "请选择文件", Toast.LENGTH_LONG).show();
}
}
}
public File saveFile(Bitmap bm, String fileName) throws IOException {
//将Bitmap类型的图片转化成file类型,便于上传到服务器
String path = Environment.getExternalStorageDirectory() + "/Ask";
File dirFile = new File(path);
if (!dirFile.exists()) {
dirFile.mkdir();
}
File myCaptureFile = new File(path + fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
return myCaptureFile;
}