Android Image Viewer using Glide library

The android image viewer using Glide library tutorial is the fifth part to the android image viewer tutorial series.

There are a couple of major issues with the android image viewer application. One is that the bitmap is being loaded on the main UI thread which will affect and delay the frames being rendered to the display. And the other is the full image size is being loaded into the bitmap which is wasteful and resulting the application consuming too much memory.

One solution would be to write your own code to create a background thread such as with an AsyncTask and then to code the in sampling resizing for loading a smaller sized bitmap.

This would involved a significant amount of coding and added complexity to the application code.

So in this android tutorial I’ve decided to choose an alternative solution which is to use an android third party image loading library.

There are a number of android third party image loading libraries available. For the android image viewer application I’ve decided to use the Glide image loading library. One of the main reasons for selecting this library is that is it one of the easiest for installing and using on android applications.

For using Glide will involve just a single API call requiring only the Uri & ImageView parameters.

Glide will also solve the main UI thread and memory issues.

 

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/android_image_viewer.git Tag image-viewer-glide

or you can run this command

git clone https://github.com/mobapptuts/android_image_viewer.git –branch image-viewer-image-viewer-glide

Code Samples

Installing the Glide image loading library

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.android.support:support-v4:23.1.0'
}

 Loading the bitmap into the ImageView using Glide

@Override
  public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
      if(requestCode == REQUEST_OPEN_RESULT_CODE && resultCode == RESULT_OK) {
          Uri uri = null;
          if(resultData != null) {
              uri = resultData.getData();
              /*
              try {
                  Bitmap bitmap = getBitmapFromUri(uri);
                  mImageView.setImageBitmap(bitmap);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              */
              Glide.with(this)
                      .load(uri)
                      .into(mImageView);
          }
      }
  }

 

Android Image Viewer using Glide library Summary

In this tutorial we learned

  • How to install an android third party library with gradle
  • The issues with loading the full size image into the bitmap
  • The impact when loading bitmaps on the main UI thread
  • How to call the Glide image loading third party library
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>