Skip to content

Commit 829c8fa

Browse files
rempawlRemigiusz Pawłowski
andauthored
Added CropException class (#344)
* added cropException * added bitmap exceptions Co-authored-by: Remigiusz Pawłowski <[email protected]>
1 parent 697c0e6 commit 829c8fa

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1010
- `Security` in case of vulnerabilities.
1111

1212
## [x.x.x] - unreleased
13+
### Changed
14+
- CropException sealed class with cancellation and Image exceptions [#332](https://github.com/CanHub/Android-Image-Cropper/issues/332)
1315
### Fixed
1416
- Fix disable closing AlertDialog when touching outside the dialog [#334](https://github.com/CanHub/Android-Image-Cropper/issues/334)
1517
## [4.2.0] - 21/03/2022

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ internal object BitmapUtils {
132132
val bitmap = decodeImage(resolver, uri, options)
133133
BitmapSampled(bitmap, options.inSampleSize)
134134
} catch (e: Exception) {
135-
throw RuntimeException(
136-
"Failed to load sampled bitmap: $uri\r\n${e.message}", e
137-
)
135+
throw CropException.FailedToLoadBitmap(uri, e.message)
138136
}
139137
}
140138

@@ -658,9 +656,7 @@ internal object BitmapUtils {
658656
result?.recycle()
659657
throw e
660658
} catch (e: Exception) {
661-
throw RuntimeException(
662-
"Failed to load sampled bitmap: $loadedImageUri\r\n${e.message}", e
663-
)
659+
throw CropException.FailedToLoadBitmap(loadedImageUri, e.message)
664660
}
665661
return BitmapSampled(result, sampleSize)
666662
}
@@ -704,7 +700,7 @@ internal object BitmapUtils {
704700
closeSafe(stream)
705701
}
706702
} while (options.inSampleSize <= 512)
707-
throw RuntimeException("Failed to decode image: $uri")
703+
throw CropException.FailedToDecodeImage(uri)
708704
}
709705

710706
/**
@@ -745,10 +741,7 @@ internal object BitmapUtils {
745741
closeSafe(stream)
746742
decoder?.recycle()
747743
} catch (e: Exception) {
748-
throw RuntimeException(
749-
"Failed to load sampled bitmap: $uri\r\n${e.message}",
750-
e
751-
)
744+
throw CropException.FailedToLoadBitmap(uri, e.message)
752745
}
753746
return BitmapSampled(null, 1)
754747
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.canhub.cropper
2+
3+
import android.net.Uri
4+
5+
sealed class CropException(message: String) : Exception(message) {
6+
class Cancellation : CropException("$EXCEPTION_PREFIX cropping has been cancelled by the user")
7+
class FailedToLoadBitmap(uri: Uri, message: String?) :
8+
CropException("$EXCEPTION_PREFIX Failed to load sampled bitmap: $uri\r\n$message")
9+
10+
class FailedToDecodeImage(uri: Uri) :
11+
CropException("$EXCEPTION_PREFIX Failed to decode image: $uri")
12+
13+
companion object {
14+
15+
const val EXCEPTION_PREFIX = "crop:"
16+
}
17+
}

cropper/src/main/java/com/canhub/cropper/CropImage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ object CropImage {
254254
originalUri = null,
255255
bitmap = null,
256256
uriContent = null,
257-
error = Exception("cropping has been cancelled by the user"),
257+
error = CropException.Cancellation(),
258258
cropPoints = floatArrayOf(),
259259
cropRect = null,
260260
wholeImageRect = null,

0 commit comments

Comments
 (0)