问题解决。
问题原因安卓7.0以上读取文件权限需要改成特定路径,改成以下代码就不会报错了。
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//步骤二:Android 7.0及以上获取文件 Uri
String authority = activity.getPackageName() + ".provider";
mUri = FileProvider.getUriForFile(activity, authority, file);
} else {
//步骤三:获取文件Uri
mUri = Uri.fromFile(file);
}
记得在清单文件中加上provider_paths
<provider android:name=".configure.bugly.BuglyFileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" tools:replace="name,authorities,exported,grantUriPermissions"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" tools:replace="name,resource" /> </provider>