2016-03-15 88 views
1

我的應用程序中有一個SqliteDatabase。在此數據庫中,我保存StingBlob(用於保存圖像)。該圖像保存在byte []的應用程序中。我轉換這個圖像與下面的代碼的幫助下,位圖:如何在Android上的Glide中設置位圖圖像

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); 

我想在Glide libary顯示此圖像,我寫了下面的代碼:

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

但是,當我運行的應用程序,出現以下錯誤:

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 

我該如何解決這個問題並在Glide庫中顯示位圖?

回答

-2

試試這個。

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

它可以幫助...

+0

謝謝@ SJ143,但是使用'.diskCacheStrategy(DiskCacheStrategy.ALL)',不知道'.asBitmap()'!!! – erere

+0

好的..只是跳過this.try剩下的 – 2016-03-15 09:52:09

+0

asBitmap()必須在載入之前()在滑翔4和之後滑翔3。 – David

7

這完全適用於我。你可以試試以下內容,

@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); 
    } 
    } 
相關問題