RxHttp 完美适配Android 10/11 上传/下载/进度监听

1、前言

随着Android 11的正式发布,适配Android 10/11 分区存储就更加的迫切了,因为Android 11开始,将强制开启分区存储,我们就无法再以绝对路径的方式去读写非沙盒目录下的文件,为此,RxHttp2.4.0版本中就正式适配了分区存储,并且,可以非常优雅的实现文件上传/下载/进度监听,三步即可搞懂任意请求。

老规矩,先看看请求三部曲 image

如果你想了解RxHttp更过功能,请查看以下系列文章

RxHttp 2000+star,协程请求,仅需三步

RxHttp 让你眼前一亮的Http请求框架

gradle依赖

//使用kapt依赖rxhttp-compiler,需要导入kapt插件
apply plugin: 'kotlin-kapt'

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                    //必须,告知RxHttp你依赖的okhttp版本,目前已适配 v3.12.0 - v4.9.0版本  (v4.3.0除外)
                    rxhttp_okhttp: '4.9.0',
                    //使用asXxx方法时必须,告知RxHttp你依赖的rxjava版本,可传入rxjava2、rxjava3
                    rxhttp_rxjava: 'rxjava3', 
                    rxhttp_package: 'rxhttp'   //非必须,指定RxHttp类包名
                ]
            }
        }
    }
    //必须,java 8或更高
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    //以下3个为必须,
    implementation 'com.ljx.rxhttp:rxhttp:2.4.1'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0' //rxhttp v2.2.2版本起,需要手动依赖okhttp
    kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.1' //生成RxHttp类,纯Java项目,请使用annotationProcessor代替kapt

    implementation 'com.ljx.rxlife:rxlife-coroutine:2.0.1' //管理协程生命周期,页面销毁,关闭请求

    //rxjava2   (RxJava2/Rxjava3二选一,使用asXxx方法时必须)
    implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'com.ljx.rxlife2:rxlife-rxjava:2.0.0' //管理RxJava2生命周期,页面销毁,关闭请求

    //rxjava3
    implementation 'io.reactivex.rxjava3:rxjava:3.0.6'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'com.ljx.rxlife3:rxlife-rxjava:3.0.0' //管理RxJava3生命周期,页面销毁,关闭请求

    //非必须,根据自己需求选择 RxHttp默认内置了GsonConverter
    implementation 'com.ljx.rxhttp:converter-fastjson:2.4.1'
    implementation 'com.ljx.rxhttp:converter-jackson:2.4.1'
    implementation 'com.ljx.rxhttp:converter-moshi:2.4.1'
    implementation 'com.ljx.rxhttp:converter-protobuf:2.4.1'
    implementation 'com.ljx.rxhttp:converter-simplexml:2.4.1'
}
复制代码

2、Android 10/11 分区存储

当我们App的targetSdkVersion更改为28以上,并且运行在Android 10以上设备时,我们无法再以绝对路径的方式,去读写非沙盒目录下的文件,当然,如果App是覆盖安装(如:targetSdkVersion 28 覆盖安装为 29),则会保持原来的访问方式。

requestLegacyExternalStorage属性

如果我们的app将targetSdkVersion更改为28以上,且想保持原来的访问方式,则需要在清单文件中将 requestLegacyExternalStorage 的值设置为 true,如下:

<manifest ...>
<!-- This attribute is "false" by default on apps targeting
     Android 10 or higher. -->
   <application android:requestLegacyExternalStorage="true" ... >
   ...
   </application>
</manifest>
复制代码

此时,便可继续以原来的方式去读写文件,然而,在Android 11上,Google又给了它新的含义,来看看官网的原话

image

也就是说,在Android 11设备上,targetSdkVersion为29以上的app,将强制开启分区存储,requestLegacyExternalStorage属性失效

注意,只要同时满足以上两个条件,不管是覆盖安装还是requestLegacyExternalStorage = true,都会强制开启分区存储

分区存储优势

  • 对用户来说,解决了文件乱放的现象
  • 对于开发者来说,我们无需写权限,就可以在分区目录下创建文件,并且访问自己创建的文件,不需要读权限(访问其它应用创建的文件,还是需要读权限)

新的文件访问方式

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值