关于Android6.0以上系统出现的拍照问题

 调用相机的时候,出现java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation 

  • 6.0以上的 Uri 形式变了

  • 下面的代码就需要进行 6.0 以上的配置,这里是 重点 , 改动有2点

    • 1.需要在自己的manifest中配置
    • 2.需要创建一个xml文件
  • 清单文件中的配置
<provider
            android:authorities="你的包名.fileprovider"
            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/provider_path" />
            ></provider>
  •   provider_path.xml内容,这个网上好多解释,自己不是很懂
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache_dir" path="."/>
    <external-cache-path name="external_dir" path="." />
</paths>
  • activity的改动 ,只需要进行下面改动
  •   private void gotoCamera() {
            Log.d("evan", "*****************打开相机********************");
            //创建拍照存储的图片文件
            tempFile = new File(FileUtils.checkDirPath(Environment.getExternalStorageDirectory().getPath() + "/image/"), System.currentTimeMillis() + ".jpg");
    
            //跳转到调用系统相机
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //设置7.0中共享文件,分享路径定义在xml/file_paths.xml
                intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                String packageName = getContext().getApplicationContext().getPackageName();
                String authority =  new StringBuilder(packageName).toString();
                Uri contentUri = FileProvider.getUriForFile(mContext, authority+ ".fileProvider", tempFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
            } else {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
            }
            startActivityForResult(intent, REQUEST_CAPTURE);
        }


猜你喜欢

转载自blog.csdn.net/zqj861791241/article/details/80492442