版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/EUEHEUEN/article/details/78855765
关于Glide4.x及其它使用,请参照:http://blog.csdn.net/guolin_blog/article/details/78582548
(4.4.0版本,对appcompat-v7的库要求比较严格,必须是27.0.2版本及以上才行)
简单使用:(使用了glide-transformations项目主页地址是: https://github.com/wasabeef/glide-transformations )
public class MyGlide4 {
public static void Load(Context context, String url, ImageView imageView) {
RequestOptions options = new RequestOptions()
// DiskCacheStrategy.NONE: 表示不缓存任何内容。
// DiskCacheStrategy.DATA: 表示只缓存原始图片。
// DiskCacheStrategy.RESOURCE: 表示只缓存转换过后的图片。
// DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片。
// DiskCacheStrategy.AUTOMATIC: 表示让Glide根据图片资源智能地选择使用哪一种缓存策略(默认选项)。
//.diskCacheStrategy(DiskCacheStrategy.NONE)//禁用缓存(这行去掉后,默认使用缓存)
// .override(400,200) //指定图片大小(像素)
// .skipMemoryCache(true) //禁用缓存
// .transforms(new BlurTransformation(), new GrayscaleTransformation()) //图片变换
// .transform(new RoundedCornersTransformation(1800,0))
.placeholder(R.drawable.load)
.error(R.mipmap.error);
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
//可以传入RequestOptions
public static void load2(Context context,
String url,
ImageView imageView,
RequestOptions options){
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
}
下面介绍两种使用Glide保存图片到本地的方法:
1.
//下载图片(Glide作为文件方式) public void downloadImage() { new Thread(new Runnable() { @Override public void run() { try { String url = "http://www.guolin.tech/book.png"; final Context context = getApplicationContext(); RequestOptions options = new RequestOptions() .override(Target.SIZE_ORIGINAL); //指定图片大小(原图) FutureTarget<File> target = Glide.with(context) .asFile() .load(url) .apply(options) .submit(); final File imageFile = target.get();//图片完整缓存路径 //将时间戳转成固定格式 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(System.currentTimeMillis()); //图片名称 final String mFileName = "/bzf/" + format.format(date) + ".jpg"; runOnUiThread(new Runnable() { @Override public void run() { String backgroundPath = imageFile.getPath(); if (backgroundPath != null) { // 其次把文件插入到系统图库 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), imageFile.getAbsolutePath(), mFileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(imageFile.getPath())))); Toast.makeText(context, "图片成功保存", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show(); } } }); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
2.
//保存图片(指定文件夹以及名称)
private void downLoadImageNew() {
final Context context = getApplicationContext();
RequestOptions options = new RequestOptions()
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); //指定图片大小(原图)
Glide.with(context).asBitmap().load(url).apply(options)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
Bitmap tempBitmip = resource;
//将时间戳转成固定格式
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
//图片名称
String mFileName = "/bzf/" + format.format(date) + ".jpg";
//图片路径
File tempPath = Environment.getExternalStorageDirectory();
//保存图片
File newFile = saveBitmap(tempPath, mFileName, tempBitmip);
String backgroundPath = newFile.getPath();
if (backgroundPath != null) {
// 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
newFile.getAbsolutePath(), backgroundPath, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(newFile.getPath()))));
Toast.makeText(context, "图片成功保存至:/storage/emulated/0/bzf", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 保存图片方法
*/
public File saveBitmap(File path, String imgName, Bitmap bitmap) {
File f = new File(path, imgName);
//创建文件夹
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return f;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}