diff --git a/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java b/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java index a66f60e890..92125514be 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java @@ -328,7 +328,11 @@ private void setPreviewImage(File photoFile) { Bitmap bitmap = BitmapShrinker.shrinkBitmap(photoFile.getAbsolutePath(), doubleScreenWidth, doubleScreenHeight); - + if (bitmap == null) { + Log.e(TAG, "Preview bitmap could not be decoded from path: " + photoFile.getAbsolutePath()); + Snackbar.make(binding.getRoot(), R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show(); + return; + } binding.photoPreview.setImageBitmap(bitmap); binding.photoPreview.setTag(savedUri); viewModel.disableTorchIfEnabled(); diff --git a/app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenImageActivity.kt b/app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenImageActivity.kt index 0614d93b60..be907c02d4 100644 --- a/app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenImageActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenImageActivity.kt @@ -142,6 +142,12 @@ class FullScreenImageActivity : AppCompatActivity() { val bitmap = BitmapShrinker.shrinkBitmap(path, doubleScreenWidth, doubleScreenHeight) + if (bitmap == null) { + Log.e(TAG, "bitmap could not be decoded from path: $path") + Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show() + return + } + val bitmapSize: Int = bitmap.byteCount // info that 100MB is the limit comes from https://stackoverflow.com/a/53334563 diff --git a/app/src/main/java/com/nextcloud/talk/utils/BitmapShrinker.kt b/app/src/main/java/com/nextcloud/talk/utils/BitmapShrinker.kt index 17db322381..1481b9a6f2 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/BitmapShrinker.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/BitmapShrinker.kt @@ -21,19 +21,25 @@ object BitmapShrinker { private const val DEGREES_270 = 270f @JvmStatic - fun shrinkBitmap(path: String, reqWidth: Int, reqHeight: Int): Bitmap { - val bitmap = decodeBitmap(path, reqWidth, reqHeight) + fun shrinkBitmap(path: String, reqWidth: Int, reqHeight: Int): Bitmap? { + val bitmap = decodeBitmap(path, reqWidth, reqHeight) ?: return null return rotateBitmap(path, bitmap) } - // solution inspired by https://developer.android.com/topic/performance/graphics/load-bitmap - private fun decodeBitmap(path: String, requestedWidth: Int, requestedHeight: Int): Bitmap = + private fun decodeBitmap(path: String, requestedWidth: Int, requestedHeight: Int): Bitmap? = BitmapFactory.Options().run { inJustDecodeBounds = true BitmapFactory.decodeFile(path, this) inSampleSize = getInSampleSize(this, requestedWidth, requestedHeight) inJustDecodeBounds = false - BitmapFactory.decodeFile(path, this) + val decodedBitmap = BitmapFactory.decodeFile(path, this) + + if (decodedBitmap == null) { + Log.e(TAG, "Failed to decode bitmap from path: $path") + // This can occur when the file is empty or corrupted, bitmap is too large. + // function does not throw an exception, but returns null + } + decodedBitmap } // solution inspired by https://developer.android.com/topic/performance/graphics/load-bitmap