摘要
《第一行代码》中的调用相机程序在新版本的Android10/11中已经不完全适用,会出现闪退等问题,此文针对需要修改的内容作出特别说明
正文
1.必须在AndroidManifest中添加CAMERA权限否则会导致闪退
<uses-permission android:name="android.permission.CAMERA"/>
2.添加provider用于文件的存储和读取
android:authorities
的参数必须与FileProvider.getUriForFile
中传入的第二个参数必须相同
android:name
的参数在Android7.0后不能使用support.v4
,使用androidx
来代替
<provider
android:authorities="com.example.cameratest.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameratest">
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraTest">
<provider
android:authorities="com.example.cameratest.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data