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

代码展示
/**
 * @date: 创建时间:2019/12/11
 * @author: gaoxiaoxiong
 * @descripion:文件操作类的监听
 **/
public interface OnFileDownListener {
  /**
     * @date :2019/12/16 0016
     * @author : gaoxiaoxiong
     * @description:
     * @param status status == -1 表示失败  status ==0 表示正在下载  status == 1 表示成功
     * @param object 返回成功后的对象参数
     * @param proGress 当前下载百分比
     * @param currentDownProGress 当前下载量
     * @param totalProGress 总的量大小
     **/
    void onFileDownStatus(int status,Object object,int proGress, long currentDownProGress, long totalProGress);
}

   /**
     * @date: 2019/5/22 0022
     * @author: gaoxiaoxiong
     * @description: 下载的图片保存的位置
     **/
    public String getPublickDiskImagePicDir() {
        return getPublickDiskFileDir(BaseApplication.getInstance(), DIRECTORY_PICTURES);
    }


    /**
     * @date: 2019/5/22 0022
     * @author: gaoxiaoxiong
     * @description: 电影保存的位置
     **/
    public String getPublickDiskMoviesDir() {
        return getPublickDiskFileDir(BaseApplication.getInstance(), DIRECTORY_MOVIES);
    }

    /**
     * @date :2019/12/16 0016
     * @author : gaoxiaoxiong
     * @description:音乐保存的位置
     **/
    public String getPublickDiskMusicDir() {
        return getPublickDiskFileDir(BaseApplication.getInstance(), DIRECTORY_MUSIC);
    }

    /**
     * @date 创建时间:2018/12/20
     * @author GaoXiaoXiong
     * @Description: 创建保存图片的缓存目录
     */
    public String getPublickDiskImagePicCacheDir() {
        return getPublickDiskCacheDir(BaseApplication.getInstance(), DIRECTORY_PICTURES);
    }

    /**
     * 作者:GaoXiaoXiong
     * 创建时间:2019/1/26
     * 注释描述:获取缓存目录
     *
     * @fileName 获取外部存储目录下缓存的 fileName的文件夹路径
     */
    public String getPublickDiskCacheDir(Context context, String fileName) {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {//此目录下的是外部存储下的私有的fileName目录
            cachePath = context.getExternalCacheDir().getPath() + "/" + fileName;  //SDCard/Android/data/你的应用包名/cache/fileName
        } else {
            cachePath = context.getCacheDir().getPath() + "/" + fileName;
        }
        File file = new File(cachePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath(); //SDCard/Android/data/你的应用包名/cache/fileName
    }

    /**
     * @date: 2019/8/2 0002
     * @author: gaoxiaoxiong
     * @description:获取外部存储目录下的 fileName的文件夹路径
     **/
    public String getPublickDiskFileDir(Context context, String fileName) {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {//此目录下的是外部存储下的私有的fileName目录
            cachePath = context.getExternalFilesDir(fileName).getAbsolutePath();  //mnt/sdcard/Android/data/com.my.app/files/fileName
        } else {
            cachePath = context.getFilesDir().getPath() + "/" + fileName;        //data/data/com.my.app/files
        }
        File file = new File(cachePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();  //mnt/sdcard/Android/data/com.my.app/files/fileName
    }


    /**
     * @date :2020/3/17 0017
     * @author : gaoxiaoxiong
     * @description:获取公共目录,注意,只适合android9.0以下的
     **/
    public String getPublickDiskFileDirAndroid9(String fileDir){
        String filePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            filePath = Environment.getExternalStoragePublicDirectory(fileDir).getPath();
        }
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }
   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() {
                @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() {
                        @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() {
            @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() {
                    @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() {

                    }
                });
    }

你可能感兴趣的:(Android 10 下载图片/视频/excel文件等,保存到公共目录)