问题:无论点击拍照后的对勾还是取消,resultCode 都为0;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_TAKE_PHOTO) {
mIvPhoto.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath));
}
}
原因:无sd卡读写权限,导致无法访问就默认为取消
解决办法:
打开相机前动态申请权限,这里使用了PermissionsDispatcher权限适配和FitAndroid7 android 7.0适配
@NeedsPermission({
Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE})
protected void takePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA).format(new Date()) + ".png";
File file = new File(Environment.getExternalStorageDirectory(), filename);
mCurrentPhotoPath = file.getAbsolutePath();
// 仅需改变这一行
Uri fileUri = FileProvider7.getUriForFile(this, file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);
}
}
仅此记录
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/6222.html