从在线获取图像时如何解决“位图无效”错误
Posted
技术标签:
【中文标题】从在线获取图像时如何解决“位图无效”错误【英文标题】:How to solve "Bitmap is not valid" error when I fetch image from online 【发布时间】:2017-03-12 11:19:12 【问题描述】:我正在尝试制作一个显示来自在线数据库的图像的应用程序。但我想使用调色板 API 从每个图像中获取鲜艳的颜色。但我想我不知道如何使用 Bitmap decodeResource 方法或 Palette。请指导我如何在在线 URL 或 URI 图像文件中使用 Bitmap factory.decoderResource 和 Palette。
这是我在活动类 recylceview 子类中使用的方法,错误显示在第 171 行“位图无效”中:
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);
);
所以这里是错误:
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文件
<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_
android:layout_
>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_
android:layout_
android:background="?colorPrimary">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_
android:layout_
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>
【问题讨论】:
【参考方案1】:抱歉回复晚了,
解决问题后我忘记回答这个问题了。所以这是答案: 我找到了用于调色板颜色的 Picasso API,这是具有调色板功能的 Picasso API 的代码
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();
);
【讨论】:
以上是关于从在线获取图像时如何解决“位图无效”错误的主要内容,如果未能解决你的问题,请参考以下文章