Skip to content

Commit 89cda1f

Browse files
authored
Merge pull request #5612 from nextcloud/bugfix/5600/fixNPE
fix NPE in BitmapShrinker
2 parents 596cc90 + daa4b25 commit 89cda1f

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,11 @@ private void setPreviewImage(File photoFile) {
328328
Bitmap bitmap = BitmapShrinker.shrinkBitmap(photoFile.getAbsolutePath(),
329329
doubleScreenWidth,
330330
doubleScreenHeight);
331-
331+
if (bitmap == null) {
332+
Log.e(TAG, "Preview bitmap could not be decoded from path: " + photoFile.getAbsolutePath());
333+
Snackbar.make(binding.getRoot(), R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show();
334+
return;
335+
}
332336
binding.photoPreview.setImageBitmap(bitmap);
333337
binding.photoPreview.setTag(savedUri);
334338
viewModel.disableTorchIfEnabled();

app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenImageActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ class FullScreenImageActivity : AppCompatActivity() {
142142

143143
val bitmap = BitmapShrinker.shrinkBitmap(path, doubleScreenWidth, doubleScreenHeight)
144144

145+
if (bitmap == null) {
146+
Log.e(TAG, "bitmap could not be decoded from path: $path")
147+
Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show()
148+
return
149+
}
150+
145151
val bitmapSize: Int = bitmap.byteCount
146152

147153
// info that 100MB is the limit comes from https://stackoverflow.com/a/53334563

app/src/main/java/com/nextcloud/talk/utils/BitmapShrinker.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ object BitmapShrinker {
2121
private const val DEGREES_270 = 270f
2222

2323
@JvmStatic
24-
fun shrinkBitmap(path: String, reqWidth: Int, reqHeight: Int): Bitmap {
25-
val bitmap = decodeBitmap(path, reqWidth, reqHeight)
24+
fun shrinkBitmap(path: String, reqWidth: Int, reqHeight: Int): Bitmap? {
25+
val bitmap = decodeBitmap(path, reqWidth, reqHeight) ?: return null
2626
return rotateBitmap(path, bitmap)
2727
}
2828

29-
// solution inspired by https://developer.android.com/topic/performance/graphics/load-bitmap
30-
private fun decodeBitmap(path: String, requestedWidth: Int, requestedHeight: Int): Bitmap =
29+
private fun decodeBitmap(path: String, requestedWidth: Int, requestedHeight: Int): Bitmap? =
3130
BitmapFactory.Options().run {
3231
inJustDecodeBounds = true
3332
BitmapFactory.decodeFile(path, this)
3433
inSampleSize = getInSampleSize(this, requestedWidth, requestedHeight)
3534
inJustDecodeBounds = false
36-
BitmapFactory.decodeFile(path, this)
35+
val decodedBitmap = BitmapFactory.decodeFile(path, this)
36+
37+
if (decodedBitmap == null) {
38+
Log.e(TAG, "Failed to decode bitmap from path: $path")
39+
// This can occur when the file is empty or corrupted, bitmap is too large.
40+
// function does not throw an exception, but returns null
41+
}
42+
decodedBitmap
3743
}
3844

3945
// solution inspired by https://developer.android.com/topic/performance/graphics/load-bitmap

0 commit comments

Comments
 (0)