Android's Glide Gets Picture Path and Glide Gets Picture Bitmap

Today, I mainly studied the use of Glide to get pictures Path and Bitmap. I believe it has troubled everyone for a long time. I also searched online for a long time, basically not. Later, I studied it and also referred to the following api documents. I summarized the following ways:

1. Get Bitmap:

1) After the image is downloaded and cached

  1. Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {  
  2.                 @Override  
  3.                 public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {  
  4.                     image.setImageBitmap(resource);  
  5.                 }  
  6.             }); //Setting asBitmap in the method can set the callback type  

Above is a simple method, below is a comprehensive method, can be perfectly controlled:

  1. Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {  
  2.                 @Override  
  3.                 public void onLoadStarted(Drawable placeholder) {  
  4.                       
  5.                 }  
  6.   
  7.                 @Override  
  8.                 public void onLoadFailed(Exception e, Drawable errorDrawable) {  
  9.   
  10.                 }  
  11.   
  12.                 @Override  
  13.                 public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {  
  14.                      //TODO set bitmap  
  15.                 }  
  16.   
  17.                 @Override  
  18.                 public void onLoadCleared(Drawable placeholder) {  
  19.   
  20.                 }  
  21.   
  22.                 @Override  
  23.                 public void getSize(SizeReadyCallback cb) {  
  24.   
  25.                 }  
  26.   
  27.                 @Override  
  28.                 public void setRequest(Request request) {  
  29.   
  30.                 }  
  31.   
  32.                 @Override  
  33.                 public Request getRequest() {  
  34.                     return null;  
  35.                 }  
  36.   
  37.                 @Override  
  38.                 public void onStart() {  
  39.   
  40.                 }  
  41.   
  42.                 @Override  
  43.                 public void onStop() {  
  44.   
  45.                 }  
  46.   
  47.                 @Override  
  48.                 public void onDestroy() {  
  49.   
  50.                 }  
  51.             });  
2) Obtain by url

  1. Bitmap myBitmap = Glide.with(applicationContext)  
  2.     .load(yourUrl)  
  3.     .asBitmap() //Must  
  4.     .centerCrop()  
  5.     .into(500500)  
  6.     .get()  

2. Get the image cache path

  1. FutureTarget<File> future = Glide.with(mContext)  
  2.                     .load("url")  
  3.                     .downloadOnly(500500);  
  4.             try {  
  5.                 File cacheFile = future.get();  
  6.                 String path = cacheFile.getAbsolutePath();  
  7.             } catch (InterruptedException e) {  
  8.                 e.printStackTrace();  
  9.             } catch (ExecutionException e) {  
  10.                 e.printStackTrace();  
  11.             }  

Note: This code needs to be executed in a thread, otherwise it will be saved.

From: http://blog.csdn .NET/qq_19711823/article/details/50856236

Posted by artin on Sat, 09 Feb 2019 11:39:17 -0800