一、从相册中选择照片:
0.声明:
public static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
public static final int CROP_PHOTO = 2; //裁剪
public static final int PHOTO_CHLICE_ALBUM = 3;// 从相册中选择
1.
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//设定结果返回
startActivityForResult(i, PHOTO_CHLICE_ALBUM);
2.
二、拍照
1.判断版本号
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.CAMERA);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mContext,
new String[]{Manifest.permission.CAMERA}, 222);
return;
} else {
//openCamra();//调用具体方法
openCamare();
mPopWindow.dismiss();
}
} else {
//openCamra();//调用具体方法
openCamare();
mPopWindow.dismiss();
}
2.打开相机
private void openCamare() { File file = new File(Environment.getExternalStorageDirectory(), "avatar.png"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Android7.0以上URI if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //添加这一句表示对目标应用临时授权该Uri所代表的文件 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //通过FileProvider创建一个content类型的Uri uriContent = FileProvider.getUriForFile(this, "com.ovov.yikao.fileprovider", file); intent.putExtra(MediaStore.EXTRA_OUTPUT, uriContent); } else { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } try { startActivityForResult(intent, PHOTO_REQUEST_CAREMA); } catch (ActivityNotFoundException anf) { Toast.makeText(this, "摄像头尚未准备好", Toast.LENGTH_SHORT).show(); } }
3.
三、共同的回调
// 照片的回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
token = (String) SPUtil.get(mContext, Contants.APP_TOKEN, "");
memid = (String) SPUtil.get(mContext, Contants.APP_MEMID, "");
decode = (String) SPUtil.get(mContext, Contants.APP_DECODE, "");
switch (requestCode) {
//从相册中选择 3
case PHOTO_CHLICE_ALBUM:
if (requestCode == PHOTO_CHLICE_ALBUM && resultCode == RESULT_OK && null != data) {
//获取返回的数据,这里是android自定义的Uri地址
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
//获取选择照片的数据视图
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
//从数据视图中获取已选择图片的路径
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
//将图片显示到界面上
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ivHeadEditinfo.setImageBitmap(bitmap);
}
break;
case PHOTO_REQUEST_CAREMA:
if (resultCode == RESULT_OK) {
tempFile = new File(Environment.getExternalStorageDirectory(), "avatar2.png");
uriOutput = Uri.fromFile(tempFile);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uriContent, "image/*");
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("crop", "true");//可裁剪
intent.putExtra("aspectX", 1); //裁剪的宽比例
intent.putExtra("aspectY", 1); //裁剪的高比例
intent.putExtra("outputX", 150); //裁剪的宽度
intent.putExtra("outputY", 150); //裁剪的高度
intent.putExtra("scale", true); //支持缩放
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriOutput); //将裁剪的结果输出到指定的Uri
intent.putExtra("return-data", true); //若为true则表示返回数据
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG);//裁剪成的图片的格式
intent.putExtra("noFaceDetection", true); //启用人脸识别
startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序
}
break;
case CROP_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uriOutput));
ivHeadEditinfo.setImageBitmap(bitmap);
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
String filename = timeStampFormat.format(new Date());
String s = uriOutput.toString();
String path = uriOutput.getPath();
//给控件赋值
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
5.