/* Android 6.0 版移除了对 Apache HTTP 客户端的支持,
从 Android 9 开始,该内容库已从 bootclasspath 中移除,且默认情况下应用无法使用它
拥有的最低 SDK 为 23 或更低的应用需要有 android:required="false" 属性,
因为在 API 级别低于 24 的设备上,org.apache.http.legacy 库不可用。(在这些设备上,Apache HTTP 类在 bootclasspath 中提供。)*/
在AndroidManifest.xml的application标签内添加
<uses-library android:name="org.apache.http.legacy" android:required="false" />
/* Android 7.0,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。
如果一项包含文件URI的intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。
要在应用间共享文件,您应发送一项 content:// URI,并授予 URI 临时访问权限。进行此授权的最简单方式是使用 FileProvider 类。 */
在AndroidManifest.xml的application标签内添加(com.xx.xx是应用包名)
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.xx.xx.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="name,authorities,exported,grantUriPermissions">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
tools:replace="name,resource" />
</provider>
资源文件res下新建xml文件夹,添加file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- 代表设备的根目录new File("/")-->
<root-path name="camera_photos" path="" />
<!--代表的目录即为:Environment.getExternalStorageDirectory()/Android/data/包名/-->
<external-path name="files_root" path="Android/data/com/xx/xx/" />
<!--代表的目录即为:Environment.getExternalStorageDirectory()-->
<external-path name="external_storage_root" path="." />
</paths>
代码适配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(context, "app的包名.fileProvider", file);
} else {
Uri uri = Uri.fromFile(file);
}
/* Android 8开始,通知需要绑定一个通道,不然就弹不出通知 */
//创建一个通知管理器
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//创建 通知通道 channelid和channelname是必须的(自己命名就好)
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(1, notification);
/* Android 9,默认启用网络传输层安全协议 (TLS)
如果您的应用以 Android 9 或更高版本为目标平台,
则 isCleartextTrafficPermitted() 方法默认返回 false。
如果您的应用需要针对特定网域启用明文,则您必须在应用的网络安全配置中,
针对这些网域明确将 cleartextTrafficPermitted 设置为 true */
在AndroidManifest.xml的application标签设置
android:networkSecurityConfig="@xml/network_security_config"
资源文件res下新建xml文件夹,添加network_security_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>