android 10 apk 存储适配,AndroidQ(10) 分区存储适配方法

创建文件夹

813aa7fed8e5b36192a396b3a133e95e.png

下载文件

InputStream is = null;

byte[] buf = new byte[2048];

int len = 0;

FileOutputStream fos = null;

OutputStream os = null;

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {

//储存下载文件的目录

File dir = new File(destFileDir);

Log.e("文件路径", "====" + dir.getPath());

if (!dir.exists()) {

dir.mkdirs();

}

file = new File(dir, destFileName);

fos = new FileOutputStream(file);

isQ = false;

} else {

Uri uri = insertFileIntoMediaStore(destFileName);

os = resolver.openOutputStream(uri);

isQ = false;

}

try {

is = response.body().byteStream();

long total = response.body().contentLength();

long sum = 0;

while ((len = is.read(buf)) != -1) {

if (isQ) {

os.write(buf, 0, len);

} else {

fos.write(buf, 0, len);

}

sum += len;

// int progress = (int) (sum * 1.0f / total * 100);

int progress = (int) (sum * 1.0f * 100 / apkSize);

//下载中更新进度条

listener.onDownloading(progress, total);

}

fos.flush();

//下载完成

listener.onDownloadSuccess(file);

} catch (Exception e) {

listener.onDownloadFailed(e);

} finally {

try {

if (is != null) {

is.close();

}

if (fos != null) {

fos.close();

}

} catch (IOException e) {

}

}

如果不想这么麻烦  只需要在manifest中添加

android:requestLegacyExternalStorage = "true"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值