Android 调用相机和相册
android开发中,调用相机拍照和选取相册图片是很常见的功能,自己就写了一个小demo,流程简单,方便参考;
实现相机功能基本思路
- 判断权限.
- 通过Intent调用相机或者获取相册图片
- 在onActivityResult回调用设置数据
判断权限
android6.0开始需要设置动态权限,Manifest中设置了权限,仍需要重新设置一下动态获取权限;
##调用了系统自带的权限检测方法
语句:ContextCompat.checkSelfPermission ( Context context, String permission);
public static int checkSelfPermission(@NonNull Context context, @NonNull String permission) {
if (permission == null) {
throw new IllegalArgumentException("permission is null");
}
return context.checkPermission(permission, android.os.Process.myPid(), Process.myUid());
}
##和权限请求方法
语句:ActivityCompat.requestPermissions ( Activity activity, String[] permissions, int requestCode) ;
public static void requestPermissions(final @NonNull Activity activity,
final @NonNull String[] permissions, final @IntRange(from = 0) int requestCode) {
if (Build.VERSION.SDK_INT >= 23) {
ActivityCompatApi23.requestPermissions(activity, permissions, requestCode);
} else if (activity instanceof OnRequestPermissionsResultCallback) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
final int[] grantResults = new int[permissions.length];
PackageManager packageManager = activity.getPackageManager();
String packageName = activity.getPackageName();
final int permissionCount = permissions.length;
for (int i = 0; i < permissionCount; i++) {
grantResults[i] = packageManager.checkPermission(
permissions[i], packageName);
}
((OnRequestPermissionsResultCallback) activity).onRequestPermissionsResult(
requestCode, permissions, grantResults);
}
});
}
}
## 请求权限的结果回调
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// 设置写入权限
if (requestCode == PERMISSION_REQUEST_TAKE_PHONE) {
// 打开了读写权限
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePhoto();
} else {
Toast.makeText(MainActivity.this, "请打开应用相机权限", Toast.LENGTH_LONG).show();
}
}
}
调用相机或者相册
调用相机直接通过 Intent 调用 MediaStroe.ACTION_IMAGE_CAPTURE 即可调用
## 创建拍照保存图片的文件
// 创建的是存放图片的文件夹
File file = new File(Environment.getExternalStorageDirectory(), "takePhotoDemo");
if (!file.exists()) {
// 如果文件路径不存在则直接创建一个文件夹
file.mkdir();
}
// 把时间作为拍摄照片的保存路径;
output = new File(file, System.currentTimeMillis() + ".jpq");
// 如果该照片已经存在就删除它,然后新创建一个
try {
if (output.exists()) {
output.delete();
}
output.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
## 调用相机
// 两种方式都可以
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 更改系统默认存储路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
## 调用相册
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CHOOSE_PHOTO);
回调参数设置
通过BitMapFactory 可以获取BiaMap图片设置到ImageView中
## 通过bitmap设置
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(photoUri));
picture.setImageBitmap(bit);