catch out of memory error thrown at rotating images before save

This commit is contained in:
tibbi 2017-06-22 22:50:41 +02:00
parent e77f211f07
commit 1c60320f5c
2 changed files with 12 additions and 4 deletions

View File

@ -32,7 +32,7 @@ android {
}
dependencies {
compile 'com.simplemobiletools:commons:2.21.11'
compile 'com.simplemobiletools:commons:2.21.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

View File

@ -75,7 +75,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
else -> 0
}
image = rotate(image, imageRot + deviceRot + previewRot)
image = rotate(image, imageRot + deviceRot + previewRot) ?: return ""
image.compress(Bitmap.CompressFormat.JPEG, 80, fos)
fos?.close()
return photoFile.absolutePath
@ -92,13 +92,21 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
return ""
}
private fun rotate(bitmap: Bitmap, degree: Int): Bitmap {
private fun rotate(bitmap: Bitmap, degree: Int): Bitmap? {
val width = bitmap.width
val height = bitmap.height
val matrix = Matrix()
matrix.setRotate((degree % 360).toFloat())
try {
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)
} catch (e: OutOfMemoryError) {
Log.e(TAG, "PhotoProcessor rotate OutOfMemoryError $e")
activity.runOnUiThread {
activity.toast(R.string.photo_not_saved)
}
}
return null
}
override fun onPostExecute(path: String) {