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 下载网络图片 保存到本地的更多相关文章

  1. Android 下载网络图片保存到本地

    通过网络地址获取网络图片,点击下载将图片显示出来,然后点击图片将图片保存到本地. 首先需要在manifest上添加一些权限: <!-- 访问网络的权限 --> <uses-permi ...

  2. C#抓取网络图片保存到本地

    C#抓取网络图片保存到本地 System.Net.WebClient myWebClient = new System.Net.WebClient(); //将头像保存到服务器 string virP ...

  3. C# 中从网络上下载文件保存到本地文件

    下面是C#中常用的从Internet上下载文件保存到本地的一些方法,没有太多的技巧. 1.通过  WebClient  类下载文件 WebClient webClient = new WebClien ...

  4. JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...

  5. h5+的Downloader下载网络图片缓存到本地的案例

    之前展示图片都是通过<img src="网络图片地址"> , 每次都请求服务器, 加载比较慢;如何做到显示图片的时候先从本地获取,没有则联网下载,缓存到本地;下次直接从 ...

  6. java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)

    最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...

  7. C#如何实现下载文件保存到本地上面去

    public void btnTemplate_Click(object sender, EventArgs e) { string strResult = string.Empty; string ...

  8. 利用scrapy下载图片保存到本地

    1.先声明一下,起始位置已经是将所有的图片链接都能到pipelines.py中 2.创建一个类,继承于ImagesPipeline,因此也就需要导入ImagesPipeline from scrapy ...

  9. php 下载保存文件保存到本地的两种方法

    第一种: 1 <? ?> 或 <?php //下载文件保存到本地//www.jbxue.comfunction downfile($fileurl){ob_start(); $fil ...

随机推荐

  1. hashlib模块(加密模块)

    hash = hashlib.md5(b"str") #md5对象,md5不能反解,可加参数 hash.update(b"str") #对字符串进行加密 has ...

  2. Jquery easyUI datagrid遇到空行做判断

    点击[上月]按钮直到没有数据,上月按钮禁用.并提示无数据. 最直接的思路就是datagrid('reload',{month:-1}); 可是这样,想了很多办法无法获取加载的数据. 最简单的办法: $ ...

  3. React native 中 SectionList用法

    一.代码 import React, { Component } from 'react'; import { AppRegistry, View, Text, SectionList, } from ...

  4. git教程:删除文件

    在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交: $ git add test.txt $ git commit -m "add test. ...

  5. 角度&弧度转换

    一.角度转换为弧度 问题: 当使用Math类的三角函数的时候,所有的单位都是用弧度表示的.你有一个或多个角是用角度数度量的,并且希望把它们转换为弧度数,从而可以用它们作为Math类的成员. 解决方法: ...

  6. 数据t转换

    #!/usr/bin/perl use strict; use warnings;    open my $fh,"a.out"; open OUT,">a_t.o ...

  7. Java基于opencv—归一化

    Opencv中提供了resize函数,可以把图像调整到相同大小 Java中resize函数的声明,内部调用的都是native方法 public static void resize(Mat src, ...

  8. JS之clientX,clientY,screenX,screenY,offsetX,offsetY区别

    首先需要知道clientX,clientY,screenX,screenY,offsetX,offsetY 是鼠标事件对象下的几个属性. 之前也一直对这些属性搞的稀里糊涂,看文档上说的也是不太理解,反 ...

  9. 关于xampp mysql字符编码与编译器编码不匹配问题

    今天,在php中对数据库字符字段进行查询的时候,语法之类的完全正确,但是就是查询不到结果,而在命令行中,同样的语句却能获得预期的功效.经多方面的了解之后才发现是字符编码不匹配的原因.在这里,把我的解决 ...

  10. Redis纠错

    在Java使用Redis的过程中遇见了一个问题, redis.clients.jedis.exceptions.JedisConnectionException: Could not get a re ...