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() {                }            });}
相關文章
相關標籤/搜索