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();
}
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("图片保存到相册失败");
}
}