相机资源使用演示
调用相机资源必须加上相机权限
private static final int REQUEST_CODE_ALBUM = 100;//打开相册
private static final int REQUEST_CODE_CAMERA = 101;//打开相机
private Uri photoUri; //相机拍照返回图片路径
private File outputImage;
new AlertDialog.Builder(getContext())
.setTitle("请选择注册方式")
.setIcon(android.R.drawable.ic_dialog_info)
.setItems(new String[]{"打开图片", "拍摄照片"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 1:
//取消严格模式 FileProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy( builder.build() );
}
//创建file对象,用于存储拍照后的图片,这也是拍照成功后的照片路径
outputImage = new File(getActivity().getExternalCacheDir(),System.currentTimeMillis()+".jpg");
try {
//判断文件是否存在,存在删除,不存在创建
if (outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
photoUri = Uri.fromFile(outputImage);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
break;
case 0:
Intent getImageByalbum = new Intent(Intent.ACTION_GET_CONTENT); //允许选择资源,并返回
getImageByalbum.addCategory(Intent.CATEGORY_OPENABLE);
getImageByalbum.setType("image/jpeg");
startActivityForResult(getImageByalbum, REQUEST_CODE_ALBUM);
break;
default:;
}
}
})
.show();
/*相机调用的回调函数,调用结束后可以在该函数中进行处理*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_ALBUM && resultCode == getActivity().RESULT_OK){
if (data != null) {
// 照片的原始资源地址
photoUri = data.getData();
String path = photoUri.getPath();
ContentResolver cr = getContext().getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(photoUri));
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(0.25f, 0.25f);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
/* 将Bitmap设定到ImageView */
imageView.setImageBitmap(bitmap1);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(), e);
}
}
}else if(requestCode == REQUEST_CODE_CAMERA && resultCode == getActivity().RESULT_OK){
//这就是拍照成功后的图片的路径,因为刚开始我们创建了这个图片的路径
photoUri= Uri.fromFile(outputImage);
//也可以转换成Bitmap
try {
Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(photoUri));
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(0.25f, 0.25f);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
imageView.setImageBitmap(bitmap1);
imageView.setBackground(null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
从数据库里取uri,并变成图片
if(userBean.getCover() != null)
{
ContentResolver cr = getContext().getContentResolver();
/*此处userBean是查询数据库返回的对象,userBean.getCover获取的就是之前存入的uri字符串*/
Uri uri = Uri.parse(userBean.getCover());
try {
Bitmap bitmap = null;
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(0.25f, 0.25f);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
imageView.setImageBitmap(bitmap1);
imageView.setBackground(null);
} catch (IOException e) {
e.printStackTrace();
}
}