I am trying to make an app which shows images from online database. but I want to use palette API to get vibrant color from each image. But I think I don't know how to use Bitmap decodeResource method OR Palette. please guide me how to use Bitmap factory.decoderResource with Palette in online URL or URI image file.
here is the method which I used in activity class recylceview subclass error shows in line number 171 "Bitmap is not valid":
170 Bitmap photo = BitmapFactory.decodeResource(getResources(), mCursor.getPosition());
171 Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getLightVibrantColor(getApplicationContext().getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
So here is the error:
10-29 17:39:42.067 31692-31750/com.example.xyzreader E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
10-29 17:39:42.282 31692-31692/com.example.xyzreader E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xyzreader, PID: 31692
java.lang.IllegalArgumentException: Bitmap is not valid
at android.support.v7.graphics.Palette$Builder.<init>(Palette.java:616)
at android.support.v7.graphics.Palette.from(Palette.java:100)
at android.support.v7.graphics.Palette.generateAsync(Palette.java:134)
at com.example.xyzreader.ui.ArticleListActivity$Adapter.onBindViewHolder(ArticleListActivity.java:171)
at com.example.xyzreader.ui.ArticleListActivity$Adapter.onBindViewHolder(ArticleListActivity.java:125)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6062)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6095)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
at android.support.v7.widget.LayoutState.next(LayoutState.java:100)
at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1568)
at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:678)
at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:600)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3374)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3183)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:630)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1142)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:836)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:850)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17519)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
10-29 17:39:42.282 31692-31692/com.example.xyzreader E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
1- ArticleListActivity.class:
public class ArticleListActivity extends ActionBarActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private Toolbar mToolbar;
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_list);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
((CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout)).setTitle("XYZ Reader");
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
getLoaderManager().initLoader(0, null, this);
if (savedInstanceState == null) {
refresh();
}
//1---- I added setOnRefreshListener and then call refresh method in onRefresh()
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});
}
private void refresh() {
startService(new Intent(this, UpdaterService.class));
}
@Override
protected void onStart() {
super.onStart();
registerReceiver(mRefreshingReceiver,
new IntentFilter(UpdaterService.BROADCAST_ACTION_STATE_CHANGE));
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(mRefreshingReceiver);
}
private boolean mIsRefreshing = false;
private BroadcastReceiver mRefreshingReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (UpdaterService.BROADCAST_ACTION_STATE_CHANGE.equals(intent.getAction())) {
mIsRefreshing = intent.getBooleanExtra(UpdaterService.EXTRA_REFRESHING, false);
updateRefreshingUI();
}
}
};
private void updateRefreshingUI() {
mSwipeRefreshLayout.setRefreshing(mIsRefreshing);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return ArticleLoader.newAllArticlesInstance(this);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
Adapter adapter = new Adapter(cursor);
adapter.setHasStableIds(true);
mRecyclerView.setAdapter(adapter);
int columnCount = getResources().getInteger(R.integer.list_column_count);
StaggeredGridLayoutManager sglm =
new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(sglm);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mRecyclerView.setAdapter(null);
}
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
private Cursor mCursor;
public Adapter(Cursor cursor) {
mCursor = cursor;
}
@Override
public long getItemId(int position) {
mCursor.moveToPosition(position);
return mCursor.getLong(ArticleLoader.Query._ID);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.list_item_article, parent, false);
final ViewHolder vh = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW,
ItemsContract.Items.buildItemUri(getItemId(vh.getAdapterPosition()))));
}
});
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
mCursor.moveToPosition(position);
holder.titleView.setText(mCursor.getString(ArticleLoader.Query.TITLE));
holder.subtitleView.setText(
DateUtils.getRelativeTimeSpanString(
mCursor.getLong(ArticleLoader.Query.PUBLISHED_DATE),
System.currentTimeMillis(), DateUtils.HOUR_IN_MILLIS,
DateUtils.FORMAT_ABBREV_ALL).toString()
+ " by "
+ mCursor.getString(ArticleLoader.Query.AUTHOR));
holder.thumbnailView.setImageUrl(
mCursor.getString(ArticleLoader.Query.THUMB_URL),
ImageLoaderHelper.getInstance(ArticleListActivity.this).getImageLoader());
holder.thumbnailView.setAspectRatio(mCursor.getFloat(ArticleLoader.Query.ASPECT_RATIO));
Bitmap photo = BitmapFactory.decodeResource(getResources(), mCursor.getPosition());
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getLightVibrantColor(getApplicationContext().getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout placeNameHolder;
public DynamicHeightNetworkImageView thumbnailView;
public TextView titleView;
public TextView subtitleView;
public ViewHolder(View view) {
super(view);
placeNameHolder=(LinearLayout) view.findViewById(R.id.placeNameHolder);
thumbnailView = (DynamicHeightNetworkImageView) view.findViewById(R.id.thumbnail);
titleView = (TextView) view.findViewById(R.id.article_title);
subtitleView = (TextView) view.findViewById(R.id.article_subtitle);
}
}
}
2- Xml file
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="112dp"
android:background="?colorPrimary">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/collapsing_toolbar_layout"
app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:expandedTitleMarginStart="72dp"
app:contentScrim="?attr/colorPrimaryDark"
app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Light"
android:layout_marginLeft="@dimen/list_toolbar_side_margin"
android:layout_marginRight="@dimen/list_toolbar_side_margin"
app:contentInsetStart="72dp"
app:layout_collapseMode="pin"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
app:layout_collapseMode="parallax"
android:scaleType="centerCrop"
android:contentDescription="@string/app_name" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
android:paddingTop="116dp"
android:paddingLeft="@dimen/list_side_margin"
android:paddingRight="@dimen/list_side_margin"
android:paddingBottom="28dp" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
Sorry for late reply,
I forgotten to answer this question after solving problem. So here is the answer : I found Picasso API for Palette color and here is the code of Picasso API with Palette feature
Picasso.with(getActivity())
.load(mCursor.getString(ArticleLoader.Query.PHOTO_URL))
.into(Imagebar, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
Bitmap bitmap = ((BitmapDrawable) Imagebar.getDrawable()).getBitmap();
if (bitmap != null) {
Palette.from(bitmap)
.generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch vibrantSwatch = palette.getDominantSwatch();
if (vibrantSwatch != null) {
bylineView.setBackgroundColor(vibrantSwatch.getRgb());
bylineView.setTextColor(vibrantSwatch.getTitleTextColor());
coordinatorLayout.setBackgroundColor(vibrantSwatch.getPopulation());
}
}
});
}
}
@Override
public void onError() {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Error on image load.", Snackbar.LENGTH_LONG);
snackbar.show();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With