java.lang.RuntimeException: Applying effect in wrong GL context! in android app while applying the filters on images

Viewed 230

I have searched for this question but couldn't find any solution to that. I am developing a photo editor app that uses a third-party library to apply effects on the images. I am showing all the filters that are available in-app in horizontal recyclerview. But when I scroll the recycler view my application crashes. Here is my adapter code.

class MyFilterAdapter ():RecyclerView.Adapter<MyFilterAdapter.MyViewHolder>(){


lateinit var filterDataSet:ArrayList<Filter>
lateinit var photoEditor1r: PhotoEditor

init {
    setUpFilters()
}
class MyViewHolder (itemview:View):RecyclerView.ViewHolder(itemview) {
   val  photoEditorViewfilter=itemview.findViewById<PhotoEditorView>(R.id.photoEdiorviewfilter)



}

override fun onCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
): MyFilterAdapter.MyViewHolder {
    val v=LayoutInflater.from(parent.context).inflate(R.layout.filter_list,parent,false)
    return MyViewHolder(v)
}



override fun getItemCount(): Int {
    return filterDataSet.size
}

override fun onBindViewHolder(holder: MyFilterAdapter.MyViewHolder, position: Int) {

    val filter:Filter=filterDataSet.get(position)
    holder.photoEditorViewfilter.source.setImageResource(filter.imgDrawableId)
    PhotoEditor.Builder(MainApplication.applicationContext(),holder.photoEditorViewfilter).build().setFilterEffect(filter.photoFilter)

}

private fun setUpFilters(){
    filterDataSet=ArrayList()

    filterDataSet.add(Filter(R.drawable.dinner, PhotoFilter.BRIGHTNESS))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.AUTO_FIX))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.CONTRAST))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.BLACK_WHITE))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.CROSS_PROCESS))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.DOCUMENTARY))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.FISH_EYE))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.FILL_LIGHT))
    filterDataSet.add(Filter(R.drawable.dinner,PhotoFilter.TINT))
}
}

Here is my error log

E/AndroidRuntime: FATAL EXCEPTION: GLThread 43113
Process: com.androidevs.photoeditor, PID: 18429
java.lang.RuntimeException: Applying effect in wrong GL context!
    at android.media.effect.EffectContext.assertValidGLState(EffectContext.java:109)
    at android.media.effect.FilterEffect.beginGLEffect(FilterEffect.java:64)
    at android.media.effect.SingleFilterEffect.apply(SingleFilterEffect.java:70)
    at ja.burhanrashid52.photoeditor.ImageFilterView.applyEffect(ImageFilterView.java:263)
    at ja.burhanrashid52.photoeditor.ImageFilterView.onDrawFrame(ImageFilterView.java:100)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1585)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1280)
1 Answers

This may happen because your Recyclerview is reusing/recycling some views.This recycled views are still held by the PhotoEditor.Builder object that referenced it,so when reused it applies the effect to the wrong viewHolder.Set your Viewholder object to viewHolder.setIsRecyclable(false); then set recyclerview.getRecycledViewpool().setMaxRecycledViews(0,0)

Related