Skip to content

Commit

Permalink
[image][Android] Try to fix recycled bitmap error (expo#21658)
Browse files Browse the repository at this point in the history
# Why

Closes expo#21488.

# How

I wasn't able to reproduce that issue. However, I have a suspicion it's caused by a race condition. For now, we can try to suppress the error. Maybe it will help. 

# Test Plan

- none 🤷
  • Loading branch information
lukmccall authored Mar 13, 2023
1 parent 124aa99 commit 620396a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/expo-image/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🐛 Bug fixes

- Fixed the `tintColor` not being passed to native view. ([#21576](https://github.com/expo/expo/pull/21576) by [@andrew-levy](https://github.com/andrew-levy))
- Fixed `canvas: trying to use a recycled bitmap` on Android. ([#21658](https://github.com/expo/expo/pull/21658) by [@lukmccall](https://github.com/lukmccall))

### 💡 Others

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.graphics.transform
import androidx.core.view.isVisible
Expand Down Expand Up @@ -179,6 +181,17 @@ class ExpoImageView(
// is used for the Outline. Unfortunately clipping is not supported
// for convex-paths and we fallback to Canvas clipping.
outlineProvider.clipCanvasIfNeeded(canvas, this)
// If we encounter a recycled bitmap here, it suggests an issue where we may have failed to
// finish clearing the image bitmap before the UI attempts to display it.
// One solution could be to suppress the error and assume that the second image view is currently responsible for displaying the correct view.
if ((drawable as? BitmapDrawable)?.bitmap?.isRecycled == true) {
Log.e("ExpoImage", "Trying to use a recycled bitmap")
recycleView()?.let { target ->
(parent as? ExpoImageViewWrapper)?.requestManager?.let { requestManager ->
target.clear(requestManager)
}
}
}
super.draw(canvas)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ExpoImageViewWrapper(context: Context, appContext: AppContext) : ExpoView(
private val activity: Activity
get() = appContext.currentActivity ?: throw MissingActivity()

private val requestManager = getOrCreateRequestManager(appContext, activity)
internal val requestManager = getOrCreateRequestManager(appContext, activity)
private val progressListener = OkHttpProgressListener(WeakReference(this))

private val firstView = ExpoImageView(activity)
Expand Down

0 comments on commit 620396a

Please sign in to comment.