AsyncTask / Bitmap / ImageView / Palette

Palette – Part 1

One of the many additions being introduced in Android-L is a new library for extracting key colours from bitmaps. In this short series we’ll take a look at the new Palette library.

palettePalette is incredibly easy to use (which is understandable considering it was written by Chris Banes who is superb at API design) and we’ll write a simple app which will extract key colours from images and display them over the selected image.

Before we begin, one word of warning: Palette is packaged within a support library which will provide backwards compatibility to API 7. However, at the time of working, out is still a Release Candidate build within the Android-L developer preview, and requires minSdkVersion 'L' in order to run. Once it is fully released I expect this restriction to be removed.

Warning out of the way, let’s move on to the code. First the layout we’ll use which consists of an ImageView and then a table of View objects which will display the various colours:

Within the Java code we’ll use a typical onCreate() method which inflates the layout and obtains references to the Views, and we also create and handle an ActionBar menu containing a single action to open an image.

The first point worthy of explanation is the image selection which is initiated in response to the use clicking the Open button in the ActionBar. The Android framework contains a file selection interface and we’ll use this. The reasoning for this is not developer laziness (although there’s no point in reinventing the wheel particularly when the Android file selector is pretty feature rich!), but to provide consistency to the user. If all apps use a standard mechanism for selecting files then it makes things much easier for users.

To do this we use startActivityForResult to kick of the file selection and we get a callback once the file selection is complete:

Next we need to load the BitmapBitmap in a simple AsyncTask:

We hold a WeakReference to PaletteActivity to ensure that we cannot leak a Context by holding a reference to it beyond its natural life-cycle (i.e. if the user exits the Activity). The Palette loading will be fairly short lived so really this shouldn’t be a problem, but it’s always worth protecting against Context-leaks, nonetheless.

Once the Bitmap load is complete, we can extract the Palette information which we’ll look at in the next article in this series.

The source code for this article is available here.

© 2014, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Palette – Part 1 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at


  1. Thanks for an article! I have a question. What if a user will change device orientation while ImageLoadTask is executing? PaletteActivity will be recreated but a new activity instance will not be updated with a loaded bitmap. I would suggest to use an event bus to notify an activity when AsyncTask is completed.

Leave a Reply

Your email address will not be published. Required fields are marked *