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 ...
随机推荐
- Laravel Scout 开启队列, 自定义queue name和queue connection
scout.php的默认配置: 'queue' => env('SCOUT_QUEUE', false), 修改为: 'queue' => [ 'queue' => env('SCO ...
- 【笔记】.NET开发环境下使用PostgreSQL+Oracle_fdw 实现两个数据库之间数据交互操作(二)
一 新的可视化工具 因为前文所提到的,看不到外部服务器和外部表的问题,我更换了可视化工具. 好用的新工具PostgreSQL Maestro! 当然如此好用的工具不是免费的,如果想免费使用还请自己去找 ...
- 刷新浏览器 protractor
//refresh browser.ignoreSynchronization = true; browser.refresh(); browser.sleep(3000); browser.swit ...
- int 和 Integer
现状1+1=?,不加思索2.有一个数字要存储在程序里,不加思索int.那为什么java要弄一个Integer类型出来?有什么用?怎么用?差别在哪儿?度娘说java提供了两种数据类型,一种是值类型,一种 ...
- DatePickerAndroid用法
一.代码/** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; import ...
- Python中使用多进程来实现并行处理的方法小结
进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...
- java数组的定义
class ArrayDome { public static void main(String[] args) { //元素类型[] 数组名 创建一个 元素类型[元素个数或数组长度] /* 需要一个 ...
- Oracle SQL Developer 连接数据库问题总结
一.使用scott用户登录的问题 说明:Oracle如果是初次使用用户,scott用户是没有解锁的.所以要先解锁.注:scott是个测试用户. 具体步骤: 1.在Dos命令下输入 sqlplus 可以 ...
- Python学习笔记,day5
Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...
- c++输出小数
#include <stdio.h> printf("%.4lf",value); #include <iomanip> cout.setf(ios::sh ...