方法1
新建/res/xml/file_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android"><external-path name="external_files" path="."/>
</paths>
配置manifest文件
<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="com.hunuo.yohoo.provider" //com.hunuo.yohoo=包名android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/>
</provider>
开始拍照
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //系统常量, 启动相机的关键
startActivityForResult(openCameraIntent, REQUEST_CODE_TAKE_PICTURE); // 参数常量为自定义的request code, 在取返回结果时有用
拍照返回图片
Bitmap bm = (Bitmap) data.getExtras().get("data");
// savePath = FileUtil.saveBitmap(bm);sczimage.setImageBitmap(bm);
裁剪图片
private void crop(file filePhoto) {
// cropImagePath = file.getAbsolutePath();Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(getImageContentUri(filePhoto), "image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 300);intent.putExtra("aspectY", 300);intent.putExtra("outputX", 300);intent.putExtra("outputY", 300);intent.putExtra("scale", true);intent.putExtra("return-data", false);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePhoto));intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection", true);startActivityForResult(intent, RESULT_REQUEST_CODE);}
3、解决三星手机拍照屏幕旋转问题
在activity下面添加
<activity android:name=".activitys.SfzShiBieActivity"
android:configChanges="keyboardHidden|orientation|screenSize"></activity>
或者
解决办法2:
在Application的onCreat()方法中添加以下代码:
// android 7.0系统解决拍照的问题StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();StrictMode.setVmPolicy(builder.build());builder.detectFileUriExposure();
判断相机
CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE); String[] cameraIds = new String[0]; try {cameraIds = manager.getCameraIdList(); } catch (CameraAccessException e) {e.printStackTrace(); } if (cameraIds != null && cameraIds.length > 0) {}else {showMyToast("请接入摄像头!"); }