@@ -17,12 +17,9 @@ import androidx.exifinterface.media.ExifInterface
17
17
import com.canhub.cropper.CropImageView.RequestSizeOptions
18
18
import com.canhub.cropper.common.CommonVersionCheck.isAtLeastQ29
19
19
import com.canhub.cropper.utils.getUriForFile
20
- import java.io.Closeable
21
20
import java.io.File
22
21
import java.io.FileNotFoundException
23
22
import java.io.IOException
24
- import java.io.InputStream
25
- import java.io.OutputStream
26
23
import java.lang.ref.WeakReference
27
24
import javax.microedition.khronos.egl.EGL10
28
25
import javax.microedition.khronos.egl.EGLConfig
@@ -426,23 +423,19 @@ internal object BitmapUtils {
426
423
compressFormat : CompressFormat ,
427
424
compressQuality : Int ,
428
425
customOutputUri : Uri ? ,
429
- ): Uri ? {
426
+ ): Uri {
430
427
val newUri = customOutputUri ? : buildUri(context, compressFormat)
431
- var outputStream: OutputStream ? = null
432
- try {
433
- outputStream = context.contentResolver.openOutputStream(newUri!! , WRITE_AND_TRUNCATE )
434
428
435
- bitmap.compress(compressFormat, compressQuality, outputStream)
436
- } finally {
437
- closeSafe(outputStream)
429
+ return context.contentResolver.openOutputStream(newUri, WRITE_AND_TRUNCATE ).use {
430
+ bitmap.compress(compressFormat, compressQuality, it)
431
+ newUri
438
432
}
439
- return newUri
440
433
}
441
434
442
435
private fun buildUri (
443
436
context : Context ,
444
437
compressFormat : CompressFormat
445
- ): Uri ? =
438
+ ): Uri =
446
439
try {
447
440
val ext = when (compressFormat) {
448
441
CompressFormat .JPEG -> " .jpg"
@@ -668,16 +661,12 @@ internal object BitmapUtils {
668
661
*/
669
662
@Throws(FileNotFoundException ::class )
670
663
private fun decodeImageForOption (resolver : ContentResolver , uri : Uri ): BitmapFactory .Options {
671
- var stream: InputStream ? = null
672
- return try {
673
- stream = resolver.openInputStream(uri)
664
+ return resolver.openInputStream(uri).use {
674
665
val options = BitmapFactory .Options ()
675
666
options.inJustDecodeBounds = true
676
- BitmapFactory .decodeStream(stream , EMPTY_RECT , options)
667
+ BitmapFactory .decodeStream(it , EMPTY_RECT , options)
677
668
options.inJustDecodeBounds = false
678
669
options
679
- } finally {
680
- closeSafe(stream)
681
670
}
682
671
}
683
672
@@ -692,14 +681,12 @@ internal object BitmapUtils {
692
681
options : BitmapFactory .Options
693
682
): Bitmap ? {
694
683
do {
695
- var stream: InputStream ? = null
696
- try {
697
- stream = resolver.openInputStream(uri)
698
- return BitmapFactory .decodeStream(stream, EMPTY_RECT , options)
699
- } catch (e: OutOfMemoryError ) {
700
- options.inSampleSize * = 2
701
- } finally {
702
- closeSafe(stream)
684
+ resolver.openInputStream(uri).use {
685
+ try {
686
+ return BitmapFactory .decodeStream(it, EMPTY_RECT , options)
687
+ } catch (e: OutOfMemoryError ) {
688
+ options.inSampleSize * = 2
689
+ }
703
690
}
704
691
} while (options.inSampleSize <= 512 )
705
692
throw CropException .FailedToDecodeImage (uri)
@@ -727,21 +714,25 @@ internal object BitmapUtils {
727
714
rect.width(), rect.height(), reqWidth, reqHeight
728
715
)
729
716
)
730
- val stream = context.contentResolver.openInputStream(uri)
731
- val decoder = BitmapRegionDecoder .newInstance(stream!! , false )
732
- do {
717
+
718
+ context.contentResolver.openInputStream(uri).use {
719
+ val decoder = BitmapRegionDecoder .newInstance(it!! , false )
720
+
733
721
try {
734
- return BitmapSampled (
735
- decoder!! .decodeRegion(rect, options),
736
- options.inSampleSize
737
- )
738
- } catch (e: OutOfMemoryError ) {
739
- options.inSampleSize * = 2
722
+ do {
723
+ try {
724
+ return BitmapSampled (
725
+ decoder!! .decodeRegion(rect, options),
726
+ options.inSampleSize
727
+ )
728
+ } catch (e: OutOfMemoryError ) {
729
+ options.inSampleSize * = 2
730
+ }
731
+ } while (options.inSampleSize <= 512 )
732
+ } finally {
733
+ decoder?.recycle()
740
734
}
741
- } while (options.inSampleSize <= 512 )
742
-
743
- closeSafe(stream)
744
- decoder?.recycle()
735
+ }
745
736
} catch (e: Exception ) {
746
737
throw CropException .FailedToLoadBitmap (uri, e.message)
747
738
}
@@ -934,19 +925,6 @@ internal object BitmapUtils {
934
925
}
935
926
}
936
927
937
- /* *
938
- * Close the given closeable object (Stream) in a safe way: check if it is null and catch-log
939
- * exception thrown.
940
- *
941
- * @param closeable the closable object to close
942
- */
943
- private fun closeSafe (closeable : Closeable ? ) {
944
- try {
945
- closeable?.close()
946
- } catch (ignored: IOException ) {
947
- }
948
- }
949
-
950
928
/* *
951
929
* Holds bitmap instance and the sample size that the bitmap was loaded/cropped with.
952
930
*/
0 commit comments