Skip to content

Commit 1b2aa82

Browse files
committed
fix(android): memory usage crashes
1 parent 723ca72 commit 1b2aa82

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

android/src/main/java/com/alpha0010/pdf/PdfView.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,15 @@ class PdfView(context: Context, private val pdfMutex: Lock) : View(context) {
221221

222222
// Api requires bitmap have alpha channel; fill with white so rendered
223223
// bitmap is opaque.
224-
val rendered = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
224+
val rendered = try {
225+
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
226+
} catch (e: OutOfMemoryError) {
227+
pdfPage.close()
228+
renderer.close()
229+
fd.close()
230+
onError("Insufficient memory to render '$mSource' at ${width}x${height}.")
231+
return@launch
232+
}
225233
rendered.eraseColor(Color.WHITE)
226234

227235
// Scale the pdf page up/down to match the requested render dimensions.

android/src/main/java/com/alpha0010/pdf/PdfViewPackage.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import com.facebook.react.uimanager.ViewManager
77
import java.util.concurrent.locks.ReentrantLock
88

99
class PdfViewPackage : ReactPackage {
10-
private val pdfMutex = ReentrantLock()
10+
companion object {
11+
private val pdfMutex = ReentrantLock()
12+
}
1113

1214
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
1315
return listOf<NativeModule>(PdfUtilModule(reactContext, pdfMutex))

0 commit comments

Comments
 (0)