comparison app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 25:648908f492c0

Fix output file names.
author David Barts <n5jrn@me.com>
date Tue, 23 Feb 2021 18:36:19 -0800
parents c29f941d09cd
children eec88eba58e9
comparison
equal deleted inserted replaced
24:474ef75856c2 25:648908f492c0
274 State.bitmap = null 274 State.bitmap = null
275 finish() 275 finish()
276 } 276 }
277 277
278 fun doneClicked(view: View): Unit { 278 fun doneClicked(view: View): Unit {
279 val contentValues = ContentValues().apply { 279 val image = State.bitmap!!
280 var fileName = getFileName(State.uri!!)
281 if (fileName == null) {
282 val d = java.util.Date()
283 fileName = "IMG_%tY%tm%td_%tH%tM%tS.jpg".format(d, d, d, d, d, d)
284 } else {
285 val dot = fileName.lastIndexOf('.')
286 if (dot == -1) {
287 fileName = fileName + ".jpg"
288 } else {
289 val fileExt = fileName.substring(dot).toLowerCase()
290 if (fileExt !in setOf(".jpg", ".jpeg"))
291 fileName = fileName.substring(0..dot) + ".jpg"
292 }
293 }
294 put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
295 put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
296 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
297 put(MediaStore.MediaColumns.RELATIVE_PATH,
298 File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).getPath())
299 }
300 }
301 binding.progressBar.visibility = ProgressBar.VISIBLE 280 binding.progressBar.visibility = ProgressBar.VISIBLE
302 ThreadPools.WORKERS.execute { 281 ThreadPools.WORKERS.execute {
282 val contentValues = ContentValues().apply {
283 var fileName = getFileName(State.uri!!)
284 if (fileName == null) {
285 val d = java.util.Date()
286 fileName = "IMG_%tY%tm%td_%tH%tM%tS".format(d, d, d, d, d, d)
287 }
288 val dot = fileName.lastIndexOf('.')
289 if (dot != -1)
290 fileName = fileName.substring(0, dot)
291 fileName = "${fileName}_${image.width}x${image.height}.jpg"
292 put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
293 put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
294 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
295 put(MediaStore.MediaColumns.RELATIVE_PATH,
296 File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).path)
297 }
298 }
303 var errorMessage: String? = null 299 var errorMessage: String? = null
304 try { 300 try {
305 val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) 301 val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
306 if (myUri == null) { 302 if (myUri == null) {
307 throw IOException(getString(R.string.error_create_mediastore)) 303 throw IOException(getString(R.string.error_create_mediastore))
313 val quality = maxOf(0, minOf(100, 309 val quality = maxOf(0, minOf(100,
314 PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt( 310 PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt(
315 "jpeg_quality", 85))) 311 "jpeg_quality", 85)))
316 Log.d("EditImage", "saving, jpeg_quality = $quality") 312 Log.d("EditImage", "saving, jpeg_quality = $quality")
317 stream.use { 313 stream.use {
318 if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, quality, it)) { 314 if (!image.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
319 throw IOException(getString(R.string.error_save_bitmap)) 315 throw IOException(getString(R.string.error_save_bitmap))
320 } 316 }
321 } 317 }
322 } catch (ioe: IOException) { 318 } catch (ioe: IOException) {
323 errorMessage = ioe.message ?: getString(R.string.error_io) 319 errorMessage = ioe.message ?: getString(R.string.error_io)