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 ...
随机推荐
- python day33 ,socketserver多线程传输,ftp作业
一.一个服务端连多个客户端的方法 1.服务端 import socketserver class MyServer(socketserver.BaseRequestHandler): def hand ...
- C# socket通讯 send方法记录
由于本人是Java入门的开发,在C#开发中遇到的问题,在此记录一下: 1.client端的send方法不管发送出去没发送出去,总是显示发送出去. 查资料得知,send方法是将数据发送到缓存区,并不是直 ...
- Ubuntu软件中心闪退
sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install --reinstall software-center
- ios 视图既显示阴影又有圆角实现
//- (UIView *)createTimeBG //{ // UIView *view = [[UIView alloc]init]; // view.backgroundColor ...
- python学习笔记——(三)文件操作
·集合操作及其相应的操作符表示集合中没有插入,只有添加,因为毕竟无序 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Vergil Zhan ...
- 华莱士的 第一个python程序之(用户登录)
Name = "shangzb"password = 43while True: _Name = input("Name:") if _Name == Name ...
- 浅析toString()和toLocaleString()的区别
翻看<javascript高级程序设计>,对toString()和toLocaleString()两者的区别并没有直白的说明,简单查阅了一下 ecmascript官方文档 和 MDN 二者 ...
- Unity攻略
Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- vue+axios跨域解决方法
通过这种方法也可以解决跨域的问题. 使用http-proxy-middleware 代理解决(项目使用vue-cli脚手架搭建) 例如请求的url:“http://f.apiplus.cn/bj11x ...
- 关于笔记本安装parrot和kali的一些问题(花屏,息屏,屏幕不能休眠)
新入手了个笔记本,还是想跟原来一样装回熟悉的kali环境中,结果我的天啊,这一路坑,简直了. 写下我遇到的问题吧,算是给大家提供一些解决方法. 1.安装kali和parrot出现无法引导的grub的情 ...