Skip to content

Commit 1c60320

Browse files
committed
catch out of memory error thrown at rotating images before save
1 parent e77f211 commit 1c60320

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ android {
3232
}
3333

3434
dependencies {
35-
compile 'com.simplemobiletools:commons:2.21.11'
35+
compile 'com.simplemobiletools:commons:2.21.12'
3636
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3737
}
3838

app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
7575
else -> 0
7676
}
7777

78-
image = rotate(image, imageRot + deviceRot + previewRot)
78+
image = rotate(image, imageRot + deviceRot + previewRot) ?: return ""
7979
image.compress(Bitmap.CompressFormat.JPEG, 80, fos)
8080
fos?.close()
8181
return photoFile.absolutePath
@@ -92,13 +92,21 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
9292
return ""
9393
}
9494

95-
private fun rotate(bitmap: Bitmap, degree: Int): Bitmap {
95+
private fun rotate(bitmap: Bitmap, degree: Int): Bitmap? {
9696
val width = bitmap.width
9797
val height = bitmap.height
9898

9999
val matrix = Matrix()
100100
matrix.setRotate((degree % 360).toFloat())
101-
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)
101+
try {
102+
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)
103+
} catch (e: OutOfMemoryError) {
104+
Log.e(TAG, "PhotoProcessor rotate OutOfMemoryError $e")
105+
activity.runOnUiThread {
106+
activity.toast(R.string.photo_not_saved)
107+
}
108+
}
109+
return null
102110
}
103111

104112
override fun onPostExecute(path: String) {

0 commit comments

Comments
 (0)