添加res/xml/file_paths.xml,用来定义文件存储路径
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="ateqi_img" path="."/>
</paths>
在AndroidManifest.xml中添加
<provider
android:authorities="${applicationId}.provider"
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>
Android7 获取Uri的方式
private fun getImageUri(context: Context, imgName: String): Uri? {
try {
val outFile = File(context.externalCacheDir, "${imgName}.jpg")
if (outFile.exists()) {
outFile.delete()
}
outFile.createNewFile()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//Android 7
FileProvider.getUriForFile(context, getCameraAuthority(context), outFile)
} else {
Uri.fromFile(outFile)
}
} catch (e: IOException) {
e.printStackTrace()
}
return null
}