/**
* 下载到本地
* @param context 上下文
* @param url 网络图
*/
private void saveImgToLocal(Context context, String url) {
//如果是网络图片,抠图的结果,需要先保存到本地
Glide.with(context)
.downloadOnly()
.load(url)
.listener(new RequestListener<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
saveToAlbum(context, resource.getAbsolutePath());
return false;
}
})
.preload();
}
/**
* 保存到相册中
* @param context 上下文
* @param srcPath 网络图保存到本地的缓存文件路径
*/
private void saveToAlbum(Context context, String srcPath) {
String dcimPath = PathUtils.getExternalDcimPath();
File file = new File(dcimPath, "content_" + System.currentTimeMillis() + ".png");
boolean isCopySuccess = FileUtils.copy(srcPath, file.getAbsolutePath());
if (isCopySuccess) {
//发送广播通知
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
ToastUtils.showShort("图片保存到相册成功");
} else {
ToastUtils.showShort("图片保存到相册失败");
}
}
Android Glide下载图片到本地(相册中)
猜你喜欢
转载自blog.csdn.net/yu540135101/article/details/111471853
今日推荐
周排行