How to set bitmap image in Glide on Android

心不动则不痛 提交于 2020-01-03 14:28:32

问题


I have a SqliteDatabase in my application. In this database, I save Sting and Blob(for save image). This image is saved in application with byte[ ]. I convert this image to Bitmap with the help of following code:

    byte[] Recycler_imageByte;
    Bitmap Recycler_theImage;

holder.Recycler_imageByte = data.get(position).getKey_image();
ByteArrayInputStream imageStream = new ByteArrayInputStream(holder.Recycler_imageByte);
holder.Recycler_theImage = BitmapFactory.decodeStream(imageStream);

I want to show this image in Glide libary, I have written the following code:

Glide.with(context).load(holder.Recycler_theImage).asBitmap().into(holder.Recycler_adapter_image);

But, when I run application, following error is shown:

03-15 10:23:14.311 22440-22440/com.tellfa.dastanak E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tellfa.dastanak, PID: 22440
java.lang.IllegalArgumentException: Unknown type class android.graphics.Bitmap. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
at com.bumptech.glide.RequestManager.load(RequestManager.java:598)
at com.tellfa.dastanak.Adapters.fragment_RecyclerAdapter.onBindViewHolder(fragment_RecyclerAdapter.java:54)
at com.tellfa.dastanak.Adapters.fragment_RecyclerAdapter.onBindViewHolder(fragment_RecyclerAdapter.java:27)
at jp.wasabeef.recyclerview.adapters.AnimationAdapter.onBindViewHolder(AnimationAdapter.java:55)
at jp.wasabeef.recyclerview.adapters.AnimationAdapter.onBindViewHolder(AnimationAdapter.java:55)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:584)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(Vi

How can I fix it and show bitmap in Glide library?


回答1:


This works perfectly for me. You can try the following ,

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;

    if (requestCode == AppConstants.REQUEST_CAMERA_PICK) {
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
      Glide.with(this)
          .load(stream.toByteArray())
          .asBitmap()
          .error(R.drawable.ic_thumb_placeholder)
          .transform(new CircleTransform(this))
          .into(imageview);
    }
  }



回答2:


Try this.

 Glide.with(this)
             .load(holder.Recycler_theImage)
             .diskCacheStrategy(DiskCacheStrategy.ALL)             
             .asBitmap().into(holder.Recycler_adapter_image);

May it help...



来源:https://stackoverflow.com/questions/36006352/how-to-set-bitmap-image-in-glide-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!