zoukankan      html  css  js  c++  java
  • Retrofit 下载网络图片 保存到本地

    private void downImage(String imagePath) {  try {
                    CommonV2Api.downloadFile(mContext, imagePath, new ICallBack<ResponseBody>() {
                        @Override
                        public void onSuccess(String flag, String key, ResponseBody responseBody) {
                            InputStream is = null;
                            FileOutputStream fos = null;
                            BufferedInputStream bis = null;
                            try {
                                int index = imagePath.lastIndexOf("/");
                                newFileName = imagePath.substring(index, imagePath.length());
                                saveImagePath = fileUtils.getPath(AppConfig.SD_DIR) +  newFileName;
                                is = responseBody.byteStream();
                                file = new File(saveImagePath);
                                if (file.exists()) {
                                    file.delete();
                                    file = new File(saveImagePath);
                                }
                                fos = new FileOutputStream(file);
                                bis = new BufferedInputStream(is);
                                byte[] buffer = new byte[1024];
                                int len;
                                while ((len = bis.read(buffer)) != -1) {
                                    fos.write(buffer, 0, len);
                                }
                                ToastUtil.show(mContext,saveImagePath);
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                if (fos != null) {
                                    try {
                                        fos.flush();
                                        fos.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (bis != null) {
                                    try {
                                        bis.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (is != null) {
                                    try {
                                        is.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
    
                        @Override
                        public void onFailure(String flag, String key, String why) {
    
                        }
                    }, false);
                } catch (Exception e) {
                    e.printStackTrace();
                } 
        }
        public static void downloadFile(Context mContext, String filePath, ICallBack<ResponseBody> callBack, Boolean boolShow, String... title) {
            Observable<ResponseBody> mObservable =
                    BuildService.getCloud().downloadFile(filePath);
            new RequestCallBack<ResponseBody>().RXResponseBody(mContext, mObservable, callBack
                    , boolShow, title);
        }
        @GET
        Observable<ResponseBody> downloadFile(@Url String fileUrl);
    public void RXResponseBody(Context mContext, Observable<T> mObservable, final ICallBack<T> callBack, Boolean boolShow, String... title){
    if (!NetUtil.isNetworkAvailable(mContext)) {
    ToastUtil.show(mContext, "请检查网络!");
    callBack.onFailure("", "404", "请检查网络!");
    return;
    }
    if (boolShow) {
    showDialog(mContext, title);
    }
    mObservable.subscribeOn(Schedulers.io())//请求数据的事件发生在io线程
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new io.reactivex.Observer<T>() {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T result) {
    if (pd != null) {
    pd.dismiss();
    }
    if (result == null) {
    callBack.onFailure("", "", "无法解析");
    } else {
    callBack.onSuccess("", "", result);
    }
    }

    @Override
    public void onError(Throwable e) {
    if (pd != null) {
    pd.dismiss();
    }
    callBack.onFailure("", "", "无法解析");
    }

    @Override
    public void onComplete() {


    }
    });
    }
  • 相关阅读:
    Elasticsearch常用插件集合(转)
    istio 安装与bookinfo示例运行(转)
    kubernetes发布tomcat服务,通过deployment,service布署(转)
    记一次ceph集群的严重故障 (转)
    Linux centos 7 安装NFS服务
    Centos7下使用Ceph-deploy快速部署Ceph分布式存储-操作记录(转)
    docker 安装 jmeter
    idea 2018.1破解激活方法,有效期至2099年
    MySQL和Oracle的区别
    SpringCloud面试题
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/10214011.html
Copyright © 2011-2022 走看看