Android 7.0 获取相机拍照图片,适配三星手机拍照,解决三星手机拍照屏幕旋转,判断设备是否有摄像头

方法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文件

 

 

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.hunuo.yohoo.provider" //com.hunuo.yohoo=包名
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android: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("请接入摄像头!");
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值