Skip to content

Commit 97cff0b

Browse files
authored
Refactoring: BitmapUtils.orientateBitmapByExif remove force unwrap. (#517)
1 parent ca853b7 commit 97cff0b

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

cropper/src/main/kotlin/com/canhub/cropper/BitmapUtils.kt

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,24 @@ internal object BitmapUtils {
7171
* If no rotation is required the image will not be rotated.<br></br>
7272
* New bitmap is created and the old one is recycled.
7373
*/
74-
fun orientateBitmapByExif(bitmap: Bitmap?, context: Context, uri: Uri?): RotateBitmapResult {
75-
var ei: ExifInterface? = null
76-
try {
77-
val `is` = context.contentResolver.openInputStream(uri!!)
78-
if (`is` != null) {
79-
ei = ExifInterface(`is`)
80-
`is`.close()
74+
fun orientateBitmapByExif(bitmap: Bitmap?, context: Context, uri: Uri): RotateBitmapResult {
75+
val exifInterface = try {
76+
context.contentResolver.openInputStream(uri)?.use {
77+
ExifInterface(it)
8178
}
82-
} catch (ignored: Exception) {
79+
} catch (ignored: Throwable) {
80+
null
81+
}
82+
83+
return when {
84+
exifInterface != null -> orientateBitmapByExif(bitmap, exifInterface)
85+
else -> RotateBitmapResult(
86+
bitmap = bitmap,
87+
degrees = 0,
88+
flipHorizontally = false,
89+
flipVertically = false,
90+
)
8391
}
84-
return if (ei != null) orientateBitmapByExif(bitmap, ei) else RotateBitmapResult(bitmap, 0)
8592
}
8693

8794
/**
@@ -105,7 +112,12 @@ internal object BitmapUtils {
105112
orientationAttributeInt == ExifInterface.ORIENTATION_TRANSPOSE
106113
val flipVertically = orientationAttributeInt == ExifInterface.ORIENTATION_FLIP_VERTICAL ||
107114
orientationAttributeInt == ExifInterface.ORIENTATION_TRANSVERSE
108-
return RotateBitmapResult(bitmap, degrees, flipHorizontally, flipVertically)
115+
return RotateBitmapResult(
116+
bitmap = bitmap,
117+
degrees = degrees,
118+
flipHorizontally = flipHorizontally,
119+
flipVertically = flipVertically,
120+
)
109121
}
110122

111123
/**
@@ -955,22 +967,15 @@ internal object BitmapUtils {
955967
}
956968
}
957969

958-
/** Holds bitmap instance and the sample size that the bitmap was loaded/cropped with. */
959970
internal class BitmapSampled(
960-
/** The bitmap instance */
961971
val bitmap: Bitmap?,
962-
/** The sample size used to lower the size of the bitmap (1,2,4,8,...) */
963972
val sampleSize: Int,
964973
)
965974

966975
internal class RotateBitmapResult(
967-
/** The loaded bitmap */
968976
val bitmap: Bitmap?,
969-
/** The degrees the image was rotated */
970977
val degrees: Int,
971-
/** If the image was flipped horizontally */
972-
val flipHorizontally: Boolean = false,
973-
/** If the image was flipped vertically */
974-
val flipVertically: Boolean = false,
978+
val flipHorizontally: Boolean,
979+
val flipVertically: Boolean,
975980
)
976981
}

0 commit comments

Comments
 (0)