/**
* 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.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);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有