一、先在AndroidManifest.xml文件中添加
< manifest>
. . .
< application>
. . .
< provider
android: name= "androidx.core.content.FileProvider"
android: authorities= "你的包名.fileprovider"
android: exported= "false"
android: grantUriPermissions= "true" >
< meta- data
android: name= "android.support.FILE_PROVIDER_PATHS"
android: resource= "@xml/file_paths" / >
< / provider>
. . .
< / application>
< / manifest>
二、在res目錄下新建xml/file_paths
< ? xml version= "1.0" encoding= "utf-8" ? >
< paths xmlns: android= "http://schemas.android.com/apk/res/android" >
< external- path name= "app" path= "" / >
< files- path name= "app" path= "" / >
< cache- path name= "app" path= "" / >
< external- cache- path name= "app" path= "" / >
< external- files- path name= "app" path= "" / >
< / paths>
三、在使用文件路徑的地方做判斷
Uri uri;
if ( Build. VERSION. SDK_INT >= Build. VERSION_CODES. N) {
uri = FileProvider. getUriForFile ( activity, 你的包名 + ".fileProvider" , file) ;
} else {
uri = Uri. fromFile ( file) ;
}