Android 10 下载图片/视频/excel文件等,保存到公共目录
文章参考
AndroidQ 适配-存储空间篇
Android Q 要来了,给你一份很"全面"的适配指南!
适配Android Q拍照和读取相册图片
Android-Q适配-存储方式
Android Q 沙箱适配多媒体文件总结
AndroidQ 适配-存储空间篇
Android Q 沙箱适配多媒体文件总结
Android Q 存储机制大变化
Android Q(10) 文件存储适配
博客内容新增
2019-12-20
1、将公共目录文件保存在沙盒目录下面
2020-03-17
1、修改下载文件到公共目录文件夹下,不仅仅可以下载Music、Movies、Pictures、还可以下载普通的excel文件等
最近升级到anroid10,google对
手机根目录文件夹的创建
越来越严格了,我倒是觉得这个是一件好事情,但是!适配是个头疼的问题。我目前处理 android<10 和 >=10 的情况,下载进度监听。
代码太多,只展示部分
,剩下代码请到
github
public static final int LOADING = 0;//加载中
public static final int SUCCESS=1;
public static final int FAIL=-1;
* 如果是要存放到沙盒外部目录,就需要使用此方法
* @date: 创建时间:2019/12/11
* @author: gaoxiaoxiong
* @descripion: 保存图片,视频,音乐到公共地区,此操作需要在线程,不是我们自己的APP目录下面的
* @param downPathUrl 下载文件的路径,需要包含后缀
* @param inserType 存储类型,可选参数 DIRECTORY_PICTURES ,DIRECTORY_MOVIES ,DIRECTORY_MUSIC ,DIRECTORY_DOWNLOADS
public void downFileFromServiceToPublicDir(String downPathUrl, Context context, String inserType, OnFileDownListener onFileDownListener) {
if (inserType.equals(DIRECTORY_DOWNLOADS)){
if (Build.VERSION.SDK_INT>=29){//android 10
downUnKnowFileFromService(downPathUrl,context,inserType,onFileDownListener);//返回的是uri
}else {
downUnKnowFileFromService(downPathUrl,onFileDownListener);//返回的是file
}else {
//下载到沙盒外部公共目录
downMusicVideoPicFromService(downPathUrl,context,inserType,onFileDownListener);
* 如果是要存放到沙盒外部目录,就需要使用此方法
* @date: 创建时间:2019/12/11
* @author: gaoxiaoxiong
* @descripion: 下载的文件到 DIRECTORY_DOWNLOADS,只有10以上才有 MediaStore.Downloads
* @param downPathUrl 下载文件的路径,需要包含后缀
* @param inserType 存储类型 DIRECTORY_DOWNLOADS
private void downUnKnowFileFromService(final String downPathUrl,final Context context, String inserType,final OnFileDownListener onFileDownListener){
if (inserType.equals(DIRECTORY_DOWNLOADS)){
Observable.just(downPathUrl).subscribeOn(Schedulers.newThread()).map(new Function<String, Uri>() {
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public Uri apply(String s) throws Exception {
Uri uri = null;
try {
URL url = new URL(downPathUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30 * 1000);
InputStream is = conn.getInputStream();
long time = System.currentTimeMillis();
int code = conn.getResponseCode();
String prefix = downPathUrl.substring(downPathUrl.lastIndexOf(".") + 1);
String fileName = null;
if (code == HttpURLConnection.HTTP_OK) {
fileName = conn.getHeaderField("Content-Disposition");
// 通过Content-Disposition获取文件名,这点跟服务器有关,需要灵活变通
if (fileName == null || fileName.length() < 1) {
// 通过截取URL来获取文件名
URL downloadUrl = conn.getURL(); // 获得实际下载文件的URL
fileName = downloadUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
} else {
fileName = URLDecoder.decode(fileName.substring(
fileName.indexOf("filename=") + 9), "UTF-8");
// 有些文件名会被包含在""里面,所以要去掉,不然无法读取文件后缀
fileName = fileName.replaceAll("\"", "");
if (isEmpty(fileName)) {
fileName = time + "." + prefix;
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Downloads.MIME_TYPE,getMIMEType(fileName));
contentValues.put(MediaStore.Downloads.DATE_TAKEN, System.currentTimeMillis());
uri = context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
BufferedInputStream inputStream = new BufferedInputStream(is);
OutputStream os = context.getContentResolver().openOutputStream(uri);
if (os != null) {
byte[] buffer = new byte[1024];
int len;
int total = 0;
int contentLeng = conn.getContentLength();
while ((len = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, len);
total += len;
if (onFileDownListener != null) {
onFileDownListener.onFileDownStatus(LOADING, null, (total * 100 / contentLeng), total, contentLeng);
os.flush();
inputStream.close();
is.close();
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return uri;
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Uri>() {
@Override
public void onSubscribe(Disposable d) {
@Override
public void onNext(Uri uri) {
if (uri != null && onFileDownListener != null) {
onFileDownListener.onFileDownStatus(SUCCESS, uri, 0, 0, 0);
} else {
onFileDownListener.onFileDownStatus(FAIL, null, 0, 0, 0);
@Override
public void onError(Throwable e) {
@Override
public void onComplete() {
* 如果是要存放到沙盒外部目录,就需要使用此方法
* @date: 创建时间:2019/12/11
* @author: gaoxiaoxiong
* @descripion: 保存图片,视频,音乐到公共地区,此操作需要在线程,不是我们自己的APP目录下面的
* @param downPathUrl 下载文件的路径,需要包含后缀
* @param inserType 存储类型,可选参数 DIRECTORY_PICTURES ,DIRECTORY_MOVIES ,DIRECTORY_MUSIC
private void downMusicVideoPicFromService(final String downPathUrl,final Context context,final String inserType,final OnFileDownListener onFileDownListener){
Observable.just(downPathUrl).subscribeOn(Schedulers.newThread()).map(new Function<String, Uri>() {
@Override
public Uri apply(String s) throws Exception {
Uri uri = null;
try {
URL url = new URL(downPathUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30 * 1000);
InputStream is = conn.getInputStream();
long time = System.currentTimeMillis();
int code = conn.getResponseCode();
String prefix = downPathUrl.substring(downPathUrl.lastIndexOf(".") + 1);
String fileName = null;
if (code == HttpURLConnection.HTTP_OK) {
fileName = conn.getHeaderField("Content-Disposition");
// 通过Content-Disposition获取文件名,这点跟服务器有关,需要灵活变通
if (fileName == null || fileName.length() < 1) {
// 通过截取URL来获取文件名
URL downloadUrl = conn.getURL(); // 获得实际下载文件的URL
fileName = downloadUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
} else {
fileName = URLDecoder.decode(fileName.substring(
fileName.indexOf("filename=") + 9), "UTF-8");
// 有些文件名会被包含在""里面,所以要去掉,不然无法读取文件后缀
fileName = fileName.replaceAll("\"", "");
if (isEmpty(fileName)) {
fileName = time + "." + prefix;
ContentValues contentValues = new ContentValues();
if (inserType.equals(DIRECTORY_PICTURES)) {
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Images.Media.MIME_TYPE, getMIMEType(fileName));
contentValues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
//只是往 MediaStore 里面插入一条新的记录,MediaStore 会返回给我们一个空的 Content Uri
//接下来问题就转化为往这个 Content Uri 里面写入
uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
} else if (inserType.equals(DIRECTORY_MOVIES)) {
contentValues.put(MediaStore.Video.Media.MIME_TYPE, getMIMEType(fileName));
contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
//只是往 MediaStore 里面插入一条新的记录,MediaStore 会返回给我们一个空的 Content Uri
//接下来问题就转化为往这个 Content Uri 里面写入
uri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
} else if (inserType.equals(DIRECTORY_MUSIC)) {
contentValues.put(MediaStore.Audio.Media.MIME_TYPE, getMIMEType(fileName));
contentValues.put(MediaStore.Audio.Media.DISPLAY_NAME, fileName);
if (Build.VERSION.SDK_INT>=29){//android 10
contentValues.put(MediaStore.Audio.Media.DATE_TAKEN, System.currentTimeMillis());
//只是往 MediaStore 里面插入一条新的记录,MediaStore 会返回给我们一个空的 Content Uri
//接下来问题就转化为往这个 Content Uri 里面写入
uri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues);
BufferedInputStream inputStream = new BufferedInputStream(is);
OutputStream os = context.getContentResolver().openOutputStream(uri);
if (os != null) {
byte[] buffer = new byte[1024];
int len;
int total = 0;
int contentLeng = conn.getContentLength();
while ((len = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, len);
total += len;
if (onFileDownListener != null) {
onFileDownListener.onFileDownStatus(LOADING, null, (total * 100 / contentLeng), total, contentLeng);
//oppo手机不会出现在照片里面,但是会出现在图集里面
if (inserType.equals(DIRECTORY_PICTURES)){//如果是图片
//扫描到相册
String[] filePathArray = FileSDCardUtil.getInstance().getPathFromContentUri(uri,context);
MediaScannerConnection.scanFile(context, new String[] {filePathArray[0]}, new String[]{"image/jpeg"}, new MediaScannerConnection.OnScanCompletedListener(){
@Override
public void onScanCompleted(String path, Uri uri) {
Log.e(TAG,"PATH:"+path);
os.flush();
inputStream.close();
is.close();
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return uri;
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Uri>() {
@Override
public void onSubscribe(Disposable d) {
@Override
public void onNext(Uri uri) {
if (uri != null && onFileDownListener != null) {
onFileDownListener.onFileDownStatus(SUCCESS, uri, 0, 0, 0);
} else {
onFileDownListener.onFileDownStatus(FAIL, null, 0, 0, 0);
@Override
public void onError(Throwable e) {
Log.e(TAG,"错误信息:"+e.getMessage());
@Override
public void onComplete() {