android快速開(kāi)發(fā)工具類
關(guān)于
Fastandrutils 是一套整理修改整合的android開(kāi)發(fā)常用的工具類。
這樣可以減少?gòu)?fù)制粘貼代碼,從而減少重復(fù)代碼,也不用為了一個(gè)常用的功能去谷歌百度,讓代碼更簡(jiǎn)潔,讓開(kāi)發(fā)更高效。
同時(shí)希望您的添加完善,讓android開(kāi)發(fā)變得更簡(jiǎn)單。
github地址,感興趣的話,不妨點(diǎn)贊支持下
個(gè)人博客
在SDK升級(jí)到Android N,通過(guò)Uri.fromFile(file)獲取Uri報(bào) android.os.FileUriExposedException異常,因?yàn)樵贏ndroid 7.0系統(tǒng)上,Android框架強(qiáng)制執(zhí)行了StrictMode API政策禁止向應(yīng)用外公開(kāi)file:// URI, 如果Intent包含了file://類型的URI離開(kāi)應(yīng)用,拋出異常,退出程序。
正確姿勢(shì)一
粗暴解決問(wèn)題
// 置入一個(gè)不設(shè)防的VmPolicy(不設(shè)置的話 7.0以上一調(diào)用拍照功能就崩潰)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
正確姿勢(shì)二
正常流程
使用FileProvider
1.配置AndroidManifest.xml
添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" /><!-- 對(duì)應(yīng)xml文件夾下的provider_paths.xml -->
</provider>
2.在res文件夾下插件xml文件夾
- xml文件夾下創(chuàng)建provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="" />
</paths>
- 代碼獲取Uri
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(getContext(), authorities, new File(filePath));
} else {
uri = Uri.fromFile(new File(filePath));
}
完畢