Im planning to get every vibrant color from each bitmap
in my RecyclerView
and set it as a background to my text. so i tried the following
:
Picasso.with(ctx)
.load(curs.getString(curs
.getColumnIndex(TimelineDatabase.KEY_IMAGE)))
.into(holder.ivUserPost, new Callback() {
@Override
public void onSuccess() {
try {
Bitmap bitmap = ((BitmapDrawable) holder.ivUserPost
.getDrawable()).getBitmap();
Palette palette = Palette.generate(bitmap);
Toast.makeText(ctx,
palette.getVibrantColor().getRgb(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
However, no toast is shown, and i keep receiving a null pointer exception
with the following
logcat:
07-27 07:13:34.163: W/System.err(4900): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.graphics.PaletteItem.getRgb()' on a null object reference
07-27 07:13:34.163: W/System.err(4900): at com.joy.timeline.Fetch$TimelineFetchAdapter$2.onSuccess(Fetch.java:354)
07-27 07:13:34.163: W/System.err(4900): at com.squareup.picasso.RequestCreator.into(RequestCreator.java:514)
07-27 07:13:34.163: W/System.err(4900): at com.joy.timeline.Fetch$TimelineFetchAdapter.onBindViewHolder(Fetch.java:344)
07-27 07:13:34.163: W/System.err(4900): at com.joy.timeline.Fetch$TimelineFetchAdapter.onBindViewHolder(Fetch.java:1)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:2925)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:2527)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:999)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:524)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1461)
07-27 07:13:34.164: W/System.err(4900): at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:1600)
07-27 07:13:34.164: W/System.err(4900): at android.view.View.layout(View.java:15273)
07-27 07:13:34.164: W/System.err(4900): at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-27 07:13:34.164: W/System.err(4900): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1069)
Obviously, Palette could not find vibrant color in your bitmap. That's why there is an NPE. You can try to ask palette to generate more colors - Palette p = Palette.generate(bitmap, 24);
. But there is also no guarantee it will find a vibrant color.
Unfortunately, there is no official Palette
documentation so far. You can refer to this article for more information