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 ...
随机推荐
- 匹配URL
使用一个不错的正则表达式来配对一个正确的url. string reg = @"(?i)(http://|https://)?(\w+\.){1,3}(com(\.cn)?|cn|net|i ...
- python基础语法二
迭代 test = "妹子有种冲我来" #可迭代对象 == 被for进行循环获取 for item in test: print(item) break #练习题: test = ...
- c# 在.NET使用Newtonsoft.Json转换,读取,写入json
转自:http://blog.sina.com.cn/s/blog_70686f3a0101kemg.html 首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https:/ ...
- AVL Tree Deletion
Overview 知识点: 1. delete函数的signature public AVLTreeNode Delete(AVLTreeNode node, int key) 2. 算法,如何删除节 ...
- docker3
Docker容器的设置资源(cpu,内存)限制: #docker run –memory=200M xxxx-image --vm 1 –verbose #docker run --cpu-s ...
- python矩阵的切片(或截取)
矩阵一般有行也有列,所以矩阵的截取也需要包含行和列两个参数. 假设a是一个矩阵,a的截取就可写成:a[起始行:终止行,起始列:终止列],中括号中有一个逗号,逗号前的是为了分割行的,逗号后的是为了分割列 ...
- javascript 之 继承
1.传统方式--->原型链 (过多继承了没用的属性) Grand.prototype.lastname = 'zhang' function Grand(); } var grand = ne ...
- 实现react路由动态加载的组件
import React, { Component } from 'react'; import Loading from '../../base/nc_Loading'; /* * date: 20 ...
- C语言-第4次作业得分
作业链接:https://edu.cnblogs.com/campus/hljkj/CS201801/homework/2523 作业链接:https://edu.cnblogs.com/campus ...
- 二、2.1 Java的下载和安装
1.下载Java 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...