@@ -61,76 +61,76 @@ class MainViewModel : ViewModel() {
61
61
bitmapInfo.apply {
62
62
if (! isExternalStorageWritable) {
63
63
onSuccess(false )
64
- cancel()
65
- }
66
-
67
- val ext = if (mime == 1 ) " webp" else if (mime == 0 ) " png" else " jpg"
68
- val explicit = resizeType == 0
69
-
70
- val tWidth = width.toIntOrNull() ? : bitmap.width
71
- val tHeight = height.toIntOrNull() ? : bitmap.height
72
-
73
- val timeStamp: String =
74
- SimpleDateFormat (" yyyyMMdd_HHmmss" , Locale .getDefault()).format(Date ())
75
- val name = " ResizedImage$timeStamp .$ext "
76
- val localBitmap = if (explicit) {
77
- Bitmap .createScaledBitmap(
78
- bitmap,
79
- tWidth,
80
- tHeight,
81
- false
82
- )
83
64
} else {
84
- bitmap.resizeBitmap(max(tWidth, tHeight))
85
- }.rotate(rotation).flip(isFlipped)
86
-
87
- val fos: OutputStream ? = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
88
- val resolver: ContentResolver = contentResolver
89
- val contentValues = ContentValues ().apply {
90
- put(MediaStore .MediaColumns .DISPLAY_NAME , name)
91
- put(MediaStore .MediaColumns .MIME_TYPE , " image/$ext " )
92
- put(MediaStore .MediaColumns .RELATIVE_PATH , " DCIM/ResizedImages" )
93
- }
94
- val imageUri =
95
- resolver.insert(
96
- MediaStore .Images .Media .EXTERNAL_CONTENT_URI ,
97
- contentValues
65
+ val ext = if (mime == 1 ) " webp" else if (mime == 0 ) " png" else " jpg"
66
+ val explicit = resizeType == 0
67
+
68
+ val tWidth = width.toIntOrNull() ? : bitmap.width
69
+ val tHeight = height.toIntOrNull() ? : bitmap.height
70
+
71
+ val timeStamp: String =
72
+ SimpleDateFormat (" yyyyMMdd_HHmmss" , Locale .getDefault()).format(Date ())
73
+ val name = " ResizedImage$timeStamp .$ext "
74
+ val localBitmap = if (explicit) {
75
+ Bitmap .createScaledBitmap(
76
+ bitmap,
77
+ tWidth,
78
+ tHeight,
79
+ false
98
80
)
99
- resolver.openOutputStream(imageUri!! )
100
- } else {
101
- val imagesDir =
102
- " ${Environment .getExternalStoragePublicDirectory(Environment .DIRECTORY_DCIM )}${File .separator} ResizedImages"
103
- val file = File (imagesDir)
104
- if (! file.exists()) {
105
- file.mkdir()
106
- }
107
- val image = File (imagesDir, " $name .$ext " )
108
- FileOutputStream (image)
81
+ } else {
82
+ bitmap.resizeBitmap(max(tWidth, tHeight))
83
+ }.rotate(rotation).flip(isFlipped)
84
+
85
+ val fos: OutputStream ? =
86
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
87
+ val resolver: ContentResolver = contentResolver
88
+ val contentValues = ContentValues ().apply {
89
+ put(MediaStore .MediaColumns .DISPLAY_NAME , name)
90
+ put(MediaStore .MediaColumns .MIME_TYPE , " image/$ext " )
91
+ put(MediaStore .MediaColumns .RELATIVE_PATH , " DCIM/ResizedImages" )
92
+ }
93
+ val imageUri =
94
+ resolver.insert(
95
+ MediaStore .Images .Media .EXTERNAL_CONTENT_URI ,
96
+ contentValues
97
+ )
98
+ resolver.openOutputStream(imageUri!! )
99
+ } else {
100
+ val imagesDir =
101
+ " ${Environment .getExternalStoragePublicDirectory(Environment .DIRECTORY_DCIM )}${File .separator} ResizedImages"
102
+ val file = File (imagesDir)
103
+ if (! file.exists()) {
104
+ file.mkdir()
105
+ }
106
+ val image = File (imagesDir, " $name .$ext " )
107
+ FileOutputStream (image)
108
+ }
109
+ localBitmap.compress(
110
+ if (mime == 1 ) Bitmap .CompressFormat .WEBP else if (mime == 0 ) Bitmap .CompressFormat .PNG else Bitmap .CompressFormat .JPEG ,
111
+ quality.toInt(),
112
+ fos
113
+ )
114
+ val out = ByteArrayOutputStream ()
115
+ localBitmap.compress(
116
+ if (mime == 1 ) Bitmap .CompressFormat .WEBP else if (mime == 0 ) Bitmap .CompressFormat .PNG else Bitmap .CompressFormat .JPEG ,
117
+ quality.toInt(), out
118
+ )
119
+ val decoded =
120
+ BitmapFactory .decodeStream(ByteArrayInputStream (out .toByteArray()))
121
+ out .flush()
122
+ out .close()
123
+ fos!! .flush()
124
+ fos.close()
125
+
126
+ _bitmap .value = decoded
127
+ _bitmapInfo .value = _bitmapInfo .value.copy(
128
+ isFlipped = false ,
129
+ rotation = 0f
130
+ )
131
+ onSuccess(true )
109
132
}
110
- localBitmap.compress(
111
- if (mime == 1 ) Bitmap .CompressFormat .WEBP else if (mime == 0 ) Bitmap .CompressFormat .PNG else Bitmap .CompressFormat .JPEG ,
112
- quality.toInt(),
113
- fos
114
- )
115
- val out = ByteArrayOutputStream ()
116
- localBitmap.compress(
117
- if (mime == 1 ) Bitmap .CompressFormat .WEBP else if (mime == 0 ) Bitmap .CompressFormat .PNG else Bitmap .CompressFormat .JPEG ,
118
- quality.toInt(), out
119
- )
120
- val decoded =
121
- BitmapFactory .decodeStream(ByteArrayInputStream (out .toByteArray()))
122
- out .flush()
123
- out .close()
124
- fos!! .flush()
125
- fos.close()
126
-
127
- _bitmap .value = decoded
128
- _bitmapInfo .value = _bitmapInfo .value.copy(
129
- isFlipped = false ,
130
- rotation = 0f
131
- )
132
133
}
133
- onSuccess(true )
134
134
}
135
135
}
136
136
}
0 commit comments