1. 获取Bitmap:
1)在图片下载缓存好之后获取
- Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
- image.setImageBitmap(resource);
- }
- }); //方法中设置<span style="font-family: Arial, Helvetica, sans-serif;">asBitmap可以设置回调类型</span>
- Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {
- @Override
- public void onLoadStarted(Drawable placeholder) {
- }
- @Override
- public void onLoadFailed(Exception e, Drawable errorDrawable) {
- }
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
- //TODO set bitmap
- }
- @Override
- public void onLoadCleared(Drawable placeholder) {
- }
- @Override
- public void getSize(SizeReadyCallback cb) {
- }
- @Override
- public void setRequest(Request request) {
- }
- @Override
- public Request getRequest() {
- return null;
- }
- @Override
- public void onStart() {
- }
- @Override
- public void onStop() {
- }
- @Override
- public void onDestroy() {
- }
- });
2)通过url获取
- Bitmap myBitmap = Glide.with(applicationContext)
- .load(yourUrl)
- .asBitmap() //必须
- .centerCrop()
- .into(500, 500)
- .get()
2. 获取图片缓存路径
- FutureTarget<File> future = Glide.with(mContext)
- .load("url")
- .downloadOnly(500, 500);
- try {
- File cacheFile = future.get();
- String path = cacheFile.getAbsolutePath();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
注意:这段代码需要在线程中执行,否则会保存,目前我还没整理出统一的方法回调,回头再研究研究。
转载自 http://blog.csdn.net/qq_19711823/article/details/50856236