适配7.0的时候,我们需要编写
permission_file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path
name="root_path"
path="." />
<files-path
name="file_path"
path="." />
<cache-path
name="cache_path"
path="." />
<external-path
name="external_path"
path="." />
<external-files-path
name="external_files_path"
path="." />
<external-cache-path
name="external_cache_path"
path="." />
<external-media-path
name="external_media_path"
path="." />
</paths>
manifrest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/permission_file_paths" />
</provider>
用到uri的时候,我们可能要转下
public static Uri toUri(String filePath) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return FileProvider.getUriForFile(App.getInstance(), App.getInstance().getPackageName() + ".fileprovider", new File(filePath));
}
return Uri.fromFile(new File(filePath));
}
有的时候,我们还需要uri转回file,这时候,我们需要适配下
public static File toFile(Uri uri) {
String pathStr = uri.getPath();
if (pathStr != null) {
//7.0路径适配问题
if (pathStr.startsWith("/external_path")) {
pathStr = pathStr.replace("/external_path", Environment.getExternalStorageDirectory().getPath());
}
File file = new File(pathStr);
return file;
}
return null;
}