安卓7.0拍照崩溃问题

0、拍照方法前加入此方法://取消严格模式 FileProvider

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();

            StrictMode.setVmPolicy(builder.build());

        }

1.在AndroidManifest.xml中配置provider  需要把com.example.android换成自己的包名

    android:name="android.support.v4.content.FileProvider"

    android:authorities="com.example.android.fileprovider"

    android:exported="false"

    android:grantUriPermissions="true">

   

        android:name="android.support.FILE_PROVIDER_PATHS"

        android:resource="@xml/file_paths">

@xml/file_paths 需要在res文件下面添加xml文件夹,下一层级添加file_paths

        name="files_root"

        path="Android/data/com.zzdj.esports.android/" />

        name="external_storage_root"

        path="." />

2、拍照的文件存储

 File的路径最好是getExternalFilesDir() or getFilesDir(),这样app被卸载,这两个文件路径也会被删除

private File createImageFile() throws IOException {

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    String imageFileName = "JPEG_" + timeStamp + "_";

    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(imageFileName, ".jpg", storageDir);

    return image;

}

你可能感兴趣的:(安卓7.0拍照崩溃问题)