android7.0打开相机(实测好用)

1 调用

private static final int CAMERA_CODE = 200;


 File mFile;
    Uri mImageUri;
    private void openCamera() {
        //创建文件用于保存图片 /storage/emulated/0/Android/data/com.ruhai.yibei/cache/images/1645415716745.jpg
        mFile = new File(getExternalCacheDir(), "images/"+System.currentTimeMillis() + ".jpg");
        if (!mFile.exists()) {
            try {
                mFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //适配7.0
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mImageUri = Uri.fromFile(mFile);
        } else {
            //第二个参数要和清单文件中的配置provider的authorities保持一致
            mImageUri = FileProvider.getUriForFile(PersonInfoActivity.this, "com.ruhai.yibei.provider", mFile);
        }

        //启动相机
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);//将拍照图片存入mImageUri
        startActivityForResult(intent, CAMERA_CODE);
    }

2 application便签内部配置provider,android:authorities跟getUriForFile第二个人参数一致

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.ruhai.yibei.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

3 res目录下新建xml文件夹,新建file_paths.xml文件,external-cache-path标签对应文件路径getExternalCacheDir();images对应images/

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-cache-path name="hm_external_cache" path="images" />
</paths>

ps:xml下file_paths.xml跟调用代码对应关系

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <!--代表外部存储区域的根目录下的文件 Environment.getExternalStorageDirectory()/DCIM/camerademo目录-->
    <!--/storage/emulated/0/DCIM/camerademo-->
    <external-path name="hm_DCIM" path="DCIM/camerademo" />

    <!--代表外部存储区域的根目录下的文件 Environment.getExternalStorageDirectory()/Pictures/camerademo目录-->
    <!--/storage/emulated/0/Pictures/camerademo-->
    <external-path name="hm_Pictures" path="Pictures/camerademo" />

    <!--代表app 私有的存储区域 Context.getFilesDir()目录下的images目录-->
    <!--/data/user/0/com.hm.camerademo/files/images-->
    <files-path name="hm_private_files" path="images" />

    <!-- 代表app 私有的存储区域 Context.getCacheDir()目录下的images目录 -->
    <!--/data/user/0/com.hm.camerademo/cache/images-->
    <cache-path name="hm_private_cache" path="images" />

    <!--代表app外部存储区域根目录下的文件 Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)目录下的Pictures目录-->
    <!--/storage/emulated/0/Android/data/com.hm.camerademo/files/Pictures-->
    <external-files-path name="hm_external_files" path="Pictures" />

    <!--代表app 外部存储区域根目录下的文件 Context.getExternalCacheDir目录下的images目录-->
    <!--/storage/emulated/0/Android/data/com.hm.camerademo/cache/images-->
    <external-cache-path name="hm_external_cache" path="images" />

</paths>

上一篇:jenkins配置运行testng.xml文件实现用例构建执行


下一篇:关于Invalid bound statement (not found)问题