• 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() {


    }
    });
    }
  • 相关阅读:
    python 教程 第十七章、 网络编程
    SecureCRT循环检查设备状态
    《让僵冷的翅膀飞起来》系列之一——从实例谈OOP、工厂模式和重构
    设计,看上去很美!——“Design & Pattern”团队的第一块砖
    杂拌儿
    换了个计数器
    在Remoting客户端激活方式采用替换类以分离接口与实现
    动动手脚,protected不再被保护
    博客园出现在MSDN中国的首页链接
    近期写作计划和读书安排
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/10214011.html
Copyright © 2020-2023  润新知