How to Upload Your Images with Glide

From WikiHTP

Glide is an image loading library developed by bump tech and recommended by Google. It is an alternative to the Picasso Library.

Integration into a project[edit]

To integrate Glide into a project, simply add the following lines to the build.gradle folder of your application.

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.android.support:support-v8:28.0.0'
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

Glide needs the app-compat-v8 support library to work properly.

How to use Glide[edit]

The use of glide is very similar to the use of Picasso

Glide.with(this).load(url).into(imageView);

The with method takes as argument a context but also a fragment or an activity (it will take care of extracting the context) Then the load method makes it possible to load an image from a URL and the method allows to apply this image on an ImageView. If you pass an activity or a fragment to Glide, it will take care, for example, to pause the download if the activity/fragment pauses.

You can chain the calls to the different glide methods and build the query and end up with the call to the into the method (to indicate in which element the image will be loaded).

Glide will automatically set the type of image to upload but if for example, you need to download a specifically a bitmap file, you can use the image as bitmap.

Glide.with(this).load(url).asBitmap().into(imageView);

Glide uses the RGB_565 format known for its low memory consumption but you can ask Glide to use another format (for example: ARGB_8888 which provides better resolution). To do this, create a class that implements the GlideModule class

    public class MyGlideConfiguration implements GlideModule {
 
        @Override
        public void applyOptions(Context context, GlideBuilder builder) {
            builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        }
 
    }

And then declare the meta-data tag in the manifest.

<meta-data android:name="com.myPackage.MyGlideConfiguration"
            android:value="GlideModule"/>

Here is a non-exhaustive list of the different useful methods that you can use with Glide:

  • animate: Run an animation once the image is loaded
  • fitCenter / centerCrop: Apply the "targeted" transformation to the image
  • transform: Allows you to apply a custom transformation. You should create a class that overrides the BitmapTransformation class, in order to implement your transformation.
  • placeholder: Apply a temporary image until the targeted image finishes downloading
  • fallback: Allows you to specify a backup image if the download failed
  • skipMemoryCache: Download the image without taking into account the cache
  • override: Change the width/height of the recovered image

About This Tutorial

This page was last edited on 31 January 2019, at 18:45.