拍照失败的原因有以下几种情况:
原因一:
没有获取相机权限,调起相机失败;
if (ContextCompat.checkSelfPermission(baseActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
// 进入这儿表示没有权限
if (ActivityCompat.shouldShowRequestPermissionRationale(baseActivity, Manifest.permission.CAMERA)) {
// 提示已经禁止
// ToastUtil.longToast(mContext, getString(R.string.you_have_cut_down_the_permission));
} else {
ActivityCompat.requestPermissions(baseActivity, new String[]{Manifest.permission.CAMERA}, 100);
}
}
原因二:
Android7.0版本:没做适配,具体代码在后面我会贴上我之前的博客
原因三:
以上都完成的情况下,拍照成功以后调起裁剪的时候 ,拿不到照片,显示加载失败。
这时候你可以看看自己的代码中有没有申请存储权限,这权限很容易疏忽导致一直拿不到图片,往往在相机回调的情况下intent返回拿到的又是空值,就容易在这纠结。其实相机回调一些手机intent值本就是返回空值null,所以我们都是通过路径拿到之前你拍成功的图片。
//获取存储权限
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(baseActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(baseActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// 提示已经禁止
} else {
ActivityCompat.requestPermissions(baseActivity, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
}
} else {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(baseActivity.getPackageManager()) != null) {
Uri fileUri = FileProvider7.getUriForFile(baseActivity, file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
baseActivity.startActivityForResult(takePictureIntent, TAKE_PICTURE);
}
}
} else {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(baseActivity.getPackageManager()) != null) {
Uri fileUri = FileProvider7.getUriForFile(baseActivity, file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
baseActivity.startActivityForResult(takePictureIntent, TAKE_PICTURE);
}
}
}
然后具体上传头的代码可以参考这篇博客:https://blog.csdn.net/Lemon_wzq/article/details/95932740