Android 7 调用系统相机

前言:

Android M相比6.0以前的需要权限,其他的跟之前的没区别:

if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)||
        (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
    //如果没有授权,则请求授权
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
File file = new File(Environment.getExternalStorageDirectory().getPath(), "camera.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // set the image file name

// start the image capture Intent
startActivityForResult(intent, 1);
而7.0开始,为了安全,需要使用FileProvider了,就不能用上面那行红色代码了:

if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)||
        (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
    //如果没有授权,则请求授权
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
    //有授权,直接开启摄像头
    //6.0 camera
    /*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File("/mnt/sdcard", "camera.png");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // set the image file name
    startActivityForResult(intent, 1);*/
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager())!=null){
        File photoFile = null;
        try {
            photoFile = createImageFile();//创建临时图片文件,方法在下面
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (photoFile != null) {
            //FileProvider 是一个特殊的 ContentProvider 的子类,
            //它使用 content:// Uri 代替了 file:/// Uri. ,更便利而且安全的为另一个app分享文件
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.sat.android.fileprovider",
                    photoFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(intent, 1);
        }
    }
}
createImageFile():

String mCurrentPhotoPath;
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    //.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    /*File storageDir = new File(Environment.getExternalStorageDirectory(), "images");
    if (!storageDir.exists()) storageDir.mkdirs();*/
    Log.d("TAH",storageDir.toString());
    //创建临时文件,文件前缀不能少于三个字符,后缀如果为空默认未".tmp"
    File image = File.createTempFile(
            imageFileName,  /* 前缀 */
            ".jpg",         /* 后缀 */
            storageDir      /* 文件夹 */
    );
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
然后需要在manifest文件中配置:

<provider
    android:authorities="com.sat.android.fileprovider" //与FileProvider.getUriForFile第二个参数相同
    android:name="android.support.v4.content.FileProvider"
android:exported="false" android:grantUriPermissions="true"><meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/></provider>
新建/res/xml/file_paths:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.sat.a10_6_/files/Pictures" />
</paths>
注意:path的值要与
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
的值一样,省略前面的 "/storage/emulatored/0/",不然会奔溃,我也不知道为什么,再研究研究。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值