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


}
});
}

 

posted on 2019-01-03 14:29  freexiaoyu  阅读(3798)  评论(0编辑  收藏  举报