Glide Media Management and Image Loading Framework

repository·master·Indexed 12 days ago

https://github.com/bumptech/glide

A fast and efficient open source media management and image loading framework for Android. Glide simplifies media decoding, memory and disk caching, and resource pooling into a single interface. Version 5.0.5 requires Android SDK API level 14 or higher and must be compiled against API 26 or later.

Tokens
1.4K
Snippets
4
Records
7
Agent score
49%

What's inside Glide

  1. How DiskLruCache works

    master

    DiskLruCache is a filesystem-based cache that uses a bounded amount of space. It stores entries consisting of a string key and a fixed number of byte-sequence values.

    Key Constraints

    • Key Format: Keys must match the regex [a-z0-9_-]{1,64}.
    • Value Size: Each value must be between 0 and Integer.MAX_VALUE bytes.
    • Directory Exclusivity: The directory used by the cache must be exclusive to that cache instance. Multiple processes must not use the same directory simultaneously, as the cache may delete or overwrite files.
    • Space Management: The cache enforces a byte limit. When exceeded, it removes entries in the background. Note that the limit is not strict (it may temporarily exceed the limit during deletion) and does not include filesystem overhead or the cache journal. It is recommended to set a conservative limit for space-sensitive applications.

    Error Handling

    • The implementation is tolerant of some I/O errors: if files are missing, entries are dropped.
    • If a write error occurs during an edit, the edit fails silently.
    • For other issues, callers must catch IOException.
  2. Glide compatibility and requirements

    master

    Before integrating Glide, ensure your project meets the following requirements:

    • Minimum Android SDK: Requires API level 14 or higher.
    • Compile Android SDK: Requires compiling against API 26 or later.
    • R8 / Proguard: Rules are automatically bundled in the AAR and interpreted by R8.

    Integration Options:

    • OkHttp 3.x: Use the okhttp3-integration optional dependency.
    • Volley: Use the volley-integration optional dependency.

    Known Limitations:

    • Round Pictures: Using CircleImageView, CircularImageView, or RoundedImageView with .crossFade() or animated GIFs may cause issues. Use .circleCrop() or .dontAnimate() to resolve this.
    • Huge Images: While Glide can downsample large images, it does not support zooming/panning ImageViews (which require tiling) out of the box.
  3. Build Glide from source

    master

    To build Glide locally using Gradle, ensure your ANDROID_HOME environment variable is set or a local.properties file exists with sdk.dir=.... You must also have the Android Support Repository installed in your Android SDK.

    git clone https://github.com/bumptech/glide.git
    cd glide
    ./gradlew jar
  4. Install Glide via Gradle or Maven

    master

    To use Glide in your Android project, add the dependency to your build configuration. Ensure you have google() and mavenCentral() in your repositories block.

    Gradle: Add the implementation line to your dependencies block.

    Maven: Add the dependency XML to your pom.xml.

    repositories {
      google()
      mavenCentral()
    }
    
    dependencies {
      implementation 'com.github.bumptech.glide:glide:5.0.5'
    }
    <dependency>
      <groupId>com.github.bumptech.glide</groupId>
      <artifactId>glide</artifactId>
      <version>5.0.5</version>
    </dependency>
  5. Read and write entries in DiskLruCache

    master

    Interacting with the cache involves two primary operations: get for reading and edit for writing/updating.

    Reading Values

    Use get to read a snapshot of an entry. The read observes the value at the moment get is called; subsequent updates or removals do not affect ongoing reads.

    Writing and Updating Values

    Use edit to create or update entries.

    • Concurrency: Only one editor can be active for an entry at a time. If an entry is currently being edited, edit will return null.
    • Creation: When creating a new entry, you must supply a full set of values (use empty values as placeholders if needed).
    • Editing: When updating an existing entry, you do not need to provide data for every value; missing values default to their previous state.
    • Atomicity: Every edit call must be followed by either Editor.commit() or Editor.abort(). Committing is atomic, ensuring readers see either the old set of values or the new set, but never a partial mix.

    Note: This implementation is specifically targeted for Android compatibility.

  6. Run Glide samples

    master

    After building the project, you can run various sample applications using Gradle tasks:

    ./gradlew :samples:flickr:run
    ./gradlew :samples:giphy:run
    ./gradlew :samples:svg:run
    ./gradlew :samples:contacturi:run
  7. Load images with Glide

    master

    Glide provides a simple fluent API to fetch and display images. You start by calling Glide.with() to bind the request to a lifecycle (like an Activity, Fragment, or Context), then use .load() to specify the source, and .into() to specify the target ImageView.

    // For a simple view:
    Glide.with(this)
         .load("https://goo.gl/gEgYUd")
         .into(imageView);
    
    // For a simple image list with transformations and placeholders:
    Glide.with(myFragment)
         .load(url)
         .centerCrop()
         .placeholder(R.drawable.loading_spinner)
         .into(myImageView);