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() {
}
});
}
Retrofit 下载网络图片 保存到本地的更多相关文章
- Android 下载网络图片保存到本地
通过网络地址获取网络图片,点击下载将图片显示出来,然后点击图片将图片保存到本地. 首先需要在manifest上添加一些权限: <!-- 访问网络的权限 --> <uses-permi ...
- C#抓取网络图片保存到本地
C#抓取网络图片保存到本地 System.Net.WebClient myWebClient = new System.Net.WebClient(); //将头像保存到服务器 string virP ...
- C# 中从网络上下载文件保存到本地文件
下面是C#中常用的从Internet上下载文件保存到本地的一些方法,没有太多的技巧. 1.通过 WebClient 类下载文件 WebClient webClient = new WebClien ...
- JAVA 通过url下载图片保存到本地
//java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...
- h5+的Downloader下载网络图片缓存到本地的案例
之前展示图片都是通过<img src="网络图片地址"> , 每次都请求服务器, 加载比较慢;如何做到显示图片的时候先从本地获取,没有则联网下载,缓存到本地;下次直接从 ...
- java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)
最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...
- C#如何实现下载文件保存到本地上面去
public void btnTemplate_Click(object sender, EventArgs e) { string strResult = string.Empty; string ...
- 利用scrapy下载图片保存到本地
1.先声明一下,起始位置已经是将所有的图片链接都能到pipelines.py中 2.创建一个类,继承于ImagesPipeline,因此也就需要导入ImagesPipeline from scrapy ...
- php 下载保存文件保存到本地的两种方法
第一种: 1 <? ?> 或 <?php //下载文件保存到本地//www.jbxue.comfunction downfile($fileurl){ob_start(); $fil ...
随机推荐
- Archlinux安装总结
Archlinux安装总结 一.引导 1.BIOS与UEFI root@archiso ~ # ls /sys/firmware/efi/efivars ls: cannot access '/sys ...
- Ubuntu16.04下ZeroC ICE的安装与使用示例(Qt C++ 和 Java)
项目需求:在Ubuntu16.04系统下安装并使用ICEgrid 3.7进行c++和Java Springboot开发环境的通信,下面逐一介绍各个步骤的详解: 一:Ice Lib的安装 参考官网地址: ...
- 微软连续12年成为Gartner分析和BI平台魔力象限的领导者
小悦还沉浸在新春开工大吉的工作中,微软Power BI就又迎来了一个好消息!据Gartner刚新鲜出炉的< 2019年Gartner的分析和商业智能平台魔力象限报告>,微软迄今已连续12 ...
- CSS3扁平化Loading动画特效
效果预览:http://hovertree.com/texiao/css3/42/ 代码如下: <!doctype html> <html> <head> < ...
- google Kickstart Round F 2017 四道题题解
Problem A. Kicksort 题意抽象一下为: 对于一个每次都从数列正中间取划分数的快速排序,给定一个1-n的排列,问快排的复杂度对于这个排列是否会退化为最坏复杂度. 数据范围: 测试组数1 ...
- bug狩猎
最近需求少了,终于有时间修一下底层的bug,做一点工具方便查bug,写篇文章简单记录一下. 一是优化了一个玩法进程的启动速度.这个玩法需要用战力做匹配,玩家按战力分段放进不同的桶里,每个桶用skipl ...
- HDU - 6203:ping ping ping (DFS序 贪心)
pro:给定N+1个点的树,有M对关键点,现在让你破坏最少的点,使得M对关键点不连通. sol:贪心,我们把M对点按照LCA深度排序,每次破坏LCA. 如果一对点(u,v,lca),u-lca-v有点 ...
- 在进行多次scanf时,printf输出错误
随便一处代码,经过改正后,输出正确的 ''' #include <stdio.h> int main(){ int T; scanf("%d",&T ...
- 关于使用deepin在linux下安装mysql出现Can't connect to local MySQL server through socket '/tmp/mysql/mysql.sock' (2)的解决方法
根据目录/etc/mysql打开文件debain.cnf 此时文本里的内容为 # Automatically generated for Debian scripts. DO NOT TOUCH![c ...
- Python学习笔记,day2
Python学习第二天 一.模块 使用模块前需在代码最前声明(import) 二.数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2 ...