android中Glide实现加载图片保存至本地并加载回调监听
作者:code小生 时间:2021-12-05 23:00:03
Glide 加载图片使用到的两个记录
Glide 加载图片保存至本地指定路径
/**
* Glide 加载图片保存到本地
*
* imgUrl 图片地址
* imgName 图片名称
*/
Glide.with(context).load(imgUrl).asBitmap().toBytes().into(new SimpleTarget<byte[]>() {
@Override
public void onResourceReady(byte[] bytes, GlideAnimation<? super byte[]> glideAnimation) {
try {
savaBitmap(imgName, bytes);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// 保存图片到手机指定目录
public void savaBitmap(String imgName, byte[] bytes) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String filePath = null;
FileOutputStream fos = null;
try {
filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/MyImg";
File imgDir = new File(filePath);
if (!imgDir.exists()) {
imgDir.mkdirs();
}
imgName = filePath + "/" + imgName;
fos = new FileOutputStream(imgName);
fos.write(bytes);
Toast.makeText(context, "图片已保存到" + filePath, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(context, "请检查SD卡是否可用", Toast.LENGTH_SHORT).show();
}
}
Glide 加载图片回调方法
Glide.with(context).load(imgUrl)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model,
Target<GlideDrawable> target,
boolean isFirstResource) {
// 可替换成进度条
Toast.makeText(context, "图片加载失败", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target,
boolean isFromMemoryCache,
boolean isFirstResource) {
// 图片加载完成,取消进度条
Toast.makeText(context, "图片加载成功", Toast.LENGTH_SHORT).show();
return false;
}
}).error(R.mipmap.ic_launcher_round)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
来源:http://www.jianshu.com/p/33d7e26e9e25?utm_source=tuicool&utm_medium=referral
标签:glide,加载,回调
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#二进制读写BinaryReader、BinaryWriter、BinaryFormatter
2022-03-07 23:01:28
spring是如何解析xml配置文件中的占位符
2023-12-02 05:57:12
SpringBoot中Mybatis + Druid 数据访问的详细过程
2023-02-22 07:20:30
![](https://img.aspxhome.com/file/2023/2/74922_0s.jpg)
SpringBoot文件上传控制及Java 获取和判断文件头信息
2021-12-10 16:47:20
![](https://img.aspxhome.com/file/2023/1/84771_0s.png)
详解Spring框架注解扫描开启之配置细节
2022-11-01 18:04:14
详解C#之事件
2022-01-07 22:18:14
![](https://img.aspxhome.com/file/2023/2/85652_0s.png)
Spring源码解析 Bean属性填充
2021-06-28 19:17:16
![](https://img.aspxhome.com/file/2023/2/61612_0s.png)
java实现五子棋小游戏
2021-12-25 06:58:56
![](https://img.aspxhome.com/file/2023/0/63380_0s.jpg)
Spring boot+beetl+i18n国际化处理的方法
2023-07-22 00:36:31
![](https://img.aspxhome.com/file/2023/5/66685_0s.png)
简单谈谈JVM、JRE和JDK的区别与联系
2023-04-20 17:14:51
![](https://img.aspxhome.com/file/2023/3/83063_0s.gif)
SpringBoot工程搭建打包、启动jar包和war包的教程图文详解
2022-05-04 22:09:10
![](https://img.aspxhome.com/file/2023/2/69892_0s.png)
兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil
2023-11-03 05:35:06
Java实战之敏感词过滤器
2022-01-18 10:32:03
![](https://img.aspxhome.com/file/2023/2/83092_0s.png)
Mybatis Plus使用XML编写动态sql的超简易方法
2022-02-20 01:49:16
![](https://img.aspxhome.com/file/2023/2/77172_0s.jpg)
C#隐式/显示实现接口方法详解
2022-01-02 02:06:30
Java SpringBoot 使用拦截器作为权限控制的实现方法
2023-04-18 01:41:58
Android消息机制Handler的工作过程详解
2023-07-31 13:49:03
![](https://img.aspxhome.com/file/2023/5/85965_0s.jpg)
Android Drawerlayout实现侧滑菜单效果
2022-10-31 19:39:23
Java中的线程同步与ThreadLocal无锁化线程封闭实现
2023-09-17 04:48:06
springboot @Async 注解如何实现方法异步
2023-11-18 18:16:03