Class Glide

java.lang.Object
com.bumptech.glide.Glide
All Implemented Interfaces:
ComponentCallbacks, ComponentCallbacks2

public class Glide extends Object implements ComponentCallbacks2
A singleton to present a simple static interface for building requests with RequestBuilder and maintaining an Engine, BitmapPool, DiskCache and MemoryCache.
  • Method Details

    • getPhotoCacheDir

      @Nullable public static File getPhotoCacheDir(@NonNull Context context)
      Returns a directory with a default name in the private cache directory of the application to use to store retrieved media and thumbnails.
      Parameters:
      context - A context.
      See Also:
    • getPhotoCacheDir

      @Nullable public static File getPhotoCacheDir(@NonNull Context context, @NonNull String cacheName)
      Returns a directory with the given name in the private cache directory of the application to use to store retrieved media and thumbnails.
      Parameters:
      context - A context.
      cacheName - The name of the subdirectory in which to store the cache.
      See Also:
    • get

      @NonNull public static Glide get(@NonNull Context context)
      Get the singleton.
      Returns:
      the singleton
    • init

      @Deprecated public static void init(Glide glide)
      Deprecated.
      Use init(Context, GlideBuilder) to get a singleton compatible with Glide's generated API.

      This method will be removed in a future version of Glide.

    • init

      public static void init(@NonNull Context context, @NonNull GlideBuilder builder)
    • isInitialized

      public static boolean isInitialized()
    • enableHardwareBitmaps

      public static void enableHardwareBitmaps()
      Allows hardware Bitmaps to be used prior to the first frame in the app being drawn as soon as this method is called.

      If you use this method in non-test code, your app will experience native crashes on some versions of Android if you try to decode a hardware Bitmap. This method is only useful for testing.

    • tearDown

      public static void tearDown()
    • getBitmapPool

      @NonNull public BitmapPool getBitmapPool()
      Returns the BitmapPool used to temporarily store Bitmaps so they can be reused to avoid garbage collections.

      Note - Using this pool directly can lead to undefined behavior and strange drawing errors. Any Bitmap added to the pool must not be currently in use in any other part of the application. Any Bitmap added to the pool must be removed from the pool before it is added a second time.

      Note - To make effective use of the pool, any Bitmap removed from the pool must eventually be re-added. Otherwise the pool will eventually empty and will not serve any useful purpose.

      The primary reason this object is exposed is for use in custom ResourceDecoders and Transformations. Use outside of these classes is not generally recommended.

    • getArrayPool

      @NonNull public ArrayPool getArrayPool()
    • getContext

      @NonNull public Context getContext()
      Returns:
      The context associated with this instance.
    • preFillBitmapPool

      public void preFillBitmapPool(@NonNull PreFillType.Builder... bitmapAttributeBuilders)
      Pre-fills the BitmapPool using the given sizes.

      Enough Bitmaps are added to completely fill the pool, so most or all of the Bitmaps currently in the pool will be evicted. Bitmaps are allocated according to the weights of the given sizes, where each size gets (weight / prefillWeightSum) percent of the pool to fill.

      Note - Pre-filling is done asynchronously using and MessageQueue.IdleHandler. Any currently running pre-fill will be cancelled and replaced by a call to this method.

      This method should be used with caution, overly aggressive pre-filling is substantially worse than not pre-filling at all. Pre-filling should only be started in onCreate to avoid constantly clearing and re-filling the BitmapPool. Rotation should be carefully considered as well. It may be worth calling this method only when no saved instance state exists so that pre-filling only happens when the Activity is first created, rather than on every rotation.

      Parameters:
      bitmapAttributeBuilders - The list of Builders representing individual sizes and configurations of Bitmaps to be pre-filled.
    • clearMemory

      public void clearMemory()
      Clears as much memory as possible.
      See Also:
    • trimMemory

      public void trimMemory(int level)
      Clears some memory with the exact amount depending on the given level.
      See Also:
    • clearDiskCache

      public void clearDiskCache()
      Clears disk cache.

      This method should always be called on a background thread, since it is a blocking call.

    • getRequestManagerRetriever

      @NonNull public RequestManagerRetriever getRequestManagerRetriever()
      Internal method.
    • setMemoryCategory

      @NonNull public MemoryCategory setMemoryCategory(@NonNull MemoryCategory memoryCategory)
      Adjusts Glide's current and maximum memory usage based on the given MemoryCategory.

      The default MemoryCategory is MemoryCategory.NORMAL. MemoryCategory.HIGH increases Glide's maximum memory usage by up to 50% and MemoryCategory.LOW decreases Glide's maximum memory usage by 50%. This method should be used to temporarily increase or decrease memory usage for a single Activity or part of the app. Use GlideBuilder.setMemoryCache(MemoryCache) to put a permanent memory size if you want to change the default.

      Returns:
      the previous MemoryCategory used by Glide.
    • with

      @NonNull public static RequestManager with(@NonNull Context context)
      Begin a load with Glide by passing in a context.

      Any requests started using a context will only have the application level options applied and will not be started or stopped based on lifecycle events. In general, loads should be started at the level the result will be used in. If the resource will be used in a view in a child fragment, the load should be started with with(android.app.Fragment)} using that child fragment. Similarly, if the resource will be used in a view in the parent fragment, the load should be started with with(android.app.Fragment) using the parent fragment. In the same vein, if the resource will be used in a view in an activity, the load should be started with with(android.app.Activity)}.

      This method is appropriate for resources that will be used outside of the normal fragment or activity lifecycle (For example in services, or for notification thumbnails).

      Parameters:
      context - Any context, will not be retained.
      Returns:
      A RequestManager for the top level application that can be used to start a load.
      See Also:
    • with

      @NonNull @Deprecated public static RequestManager with(@NonNull Activity activity)
      Deprecated.
      This is equivalent to calling with(Context) using the application context. Use the androidx Activity class instead (ie FragmentActivity, or AppCompatActivity).
      Begin a load with Glide that will be tied to the given Activity's lifecycle and that uses the given Activity's default options.
      Parameters:
      activity - The activity to use.
      Returns:
      A RequestManager for the given activity that can be used to start a load.
    • with

      @NonNull public static RequestManager with(@NonNull androidx.fragment.app.FragmentActivity activity)
      Begin a load with Glide that will tied to the give FragmentActivity's lifecycle and that uses the given FragmentActivity's default options.
      Parameters:
      activity - The activity to use.
      Returns:
      A RequestManager for the given FragmentActivity that can be used to start a load.
    • with

      @NonNull public static RequestManager with(@NonNull androidx.fragment.app.Fragment fragment)
      Begin a load with Glide that will be tied to the given Fragment's lifecycle and that uses the given Fragment's default options.
      Parameters:
      fragment - The fragment to use.
      Returns:
      A RequestManager for the given Fragment that can be used to start a load.
    • with

      @Deprecated @NonNull public static RequestManager with(@NonNull Fragment fragment)
      Deprecated.
      This method is identical to calling with(Context) using the application context. Prefer support Fragments and with(Fragment) instead. See https://github.com/android/android-ktx/pull/161#issuecomment-363270555.
      Begin a load with Glide that will be tied to the given Fragment's lifecycle and that uses the given Fragment's default options.
      Parameters:
      fragment - The fragment to use.
      Returns:
      A RequestManager for the given Fragment that can be used to start a load.
    • with

      @NonNull public static RequestManager with(@NonNull View view)
      Begin a load with Glide that will be tied to the lifecycle of the Fragment, Fragment, or Activity that contains the View.

      A Fragment or Fragment is assumed to contain a View if the View is a child of the View returned by the Fragment.getView()} method.

      This method will not work if the View is not attached. Prefer the Activity and Fragment variants unless you're loading in a View subclass.

      This method may be inefficient aways and is definitely inefficient for large hierarchies. Consider memoizing the result after the View is attached or again, prefer the Activity and Fragment variants whenever possible.

      When used in Applications that use the non-support Fragment classes, calling this method will produce noisy logs from FragmentManager. Consider avoiding entirely or using the Fragments from the support library instead.

      If the support FragmentActivity class is used, this method will only attempt to discover support Fragments. Any non-support Fragments attached to the FragmentActivity will be ignored.

      Parameters:
      view - The view to search for a containing Fragment or Activity from.
      Returns:
      A RequestManager that can be used to start a load.
    • getRegistry

      @NonNull public Registry getRegistry()
    • onTrimMemory

      public void onTrimMemory(int level)
      Specified by:
      onTrimMemory in interface ComponentCallbacks2
    • onConfigurationChanged

      public void onConfigurationChanged(Configuration newConfig)
      Specified by:
      onConfigurationChanged in interface ComponentCallbacks
    • onLowMemory

      public void onLowMemory()
      Specified by:
      onLowMemory in interface ComponentCallbacks