android10apk存储适配,AndroidQ(10)分区存储适配⽅法创建⽂件夹
下载⽂件
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("⽂件路径", "====" + Path());
if (!ists()) {
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); //下载中更新进度条
}
fos.flush();
//下载完成
} catch (Exception e) {mkdirs方法
} finally {
try {
if (is != null) {
is.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
}
}
如果不想这么⿇烦 只需要在manifest中添加android:requestLegacyExternalStorage = "true"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论