K. Sopheak K. Sopheak - 1 year ago 78
Android Question

How to detect bitmap picture contain muted or vibrant color?

I use

class to set random background color to
, and some images cannot generate color and return color like gray. I find that some image file work with
and some other work with
and others.

Here is my layout. I use
to show bitmap and a
below it to show name with random background.

How can we detect whether image file work with `getMutedColor` or `getVibrantColor` or others?

Here is my code I try so far:

public void generateColor(Bitmap mypic){
Palette p = Palette.from(result).generate();

I am appreciate for any help.

Answer Source

I'd simply check for null.

public int getSwatch(Bitmap b){
    Palette p = Palette.from(b).generate();
    Palette.Swatch swatch;
    if((swatch = p.getVibrantSwatch()) != null){
        return swatch.getRgb();
    if((swatch = p.getLightMutedSwatch()) != null){
        return swatch.getRgb();
    return Color.WHITE;

Color.WHITE at the end is the fallback color. Should Palette be unable to find a color, you will still get WHITE as a result. You can swap it with any color you wish.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download