Android Q 文件适配 网络适配

2021-09-26  本文已影响0人  没有了遇见

Android项目常见兼容适配,涉及适配 网络 ,文件

Android Q 版本以及以后版本将开启沙盒机制即App只能访问自身目录下和公共目录的资源 其他App存储的资源将访问不到(文件存在但是不能读写),建议以后文件存储到App目录下方便读取,处理其他文件用Uri 处理

1.Android Q 版本沙盒文件适配沙盒版本

配置

1.AndroidManifest.xml 中配置 android:requestLegacyExternalStorage="true"

   <application
    android:name=".application.SophixStubApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:requestLegacyExternalStorage="true"
    android:largeHeap="true"
    android:networkSecurityConfig="@xml/network_security_config"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:supportsRtl,android:label,android:theme,android:allowBackup">

2:配置 provider

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="包名.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/app_files"
            tools:replace="android:resource" />
    </provider>

3: res 文件夹中创建 xml文件 provider

<resources>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="" />
    <path>
        <root-path
            name="root_path"
            path="." />
    </path>
    <external-path
        name="camera_photos"
        path="" />
    <external-path
        name="external_storage_root"
        path="." />

</paths>

</resources>

2.网络适配:Http适配,apache包移除适配

1.Android6.0 (API 23)以后 SDK不再支持 org.apache.http.*包的解析

  1.需要在app目录下的bulid.gradle中配置(注意:是android {}下)
  useLibrary 'org.apache.http.legacy'

  2.app目录下的androidManifest.xml配置
   <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" />
bulid.gradle配置
androidManifest.xm配置

2.Google在Android P为了安全起见,已经明确规定禁止http协议额,但是之前很多接口都是http协议,兼容http协议配置

1:在res目录下xml目录下(不存在就创建xml文件夹) 创建network_security_config.xml文件

  <?xml version="1.0" encoding="utf-8"?>
   <network-security-config>
  <base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
        <certificates src="user" />
    </trust-anchors>
   </base-config>
 </network-security-config>

2:AndroidManifest.xml 中配置 android:networkSecurityConfig="@xml/network_security_config"

    <application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:requestLegacyExternalStorage="true"
    android:largeHeap="true"
    android:networkSecurityConfig="@xml/network_security_config"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:supportsRtl,android:label,android:theme,android:allowBackup">
上一篇下一篇

猜你喜欢

热点阅读