changeset 25:648908f492c0

Fix output file names.
author David Barts <n5jrn@me.com>
date Tue, 23 Feb 2021 18:36:19 -0800
parents 474ef75856c2
children 43f6534036ec
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt
diffstat 1 files changed, 18 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Tue Feb 23 15:20:15 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Tue Feb 23 18:36:19 2021 -0800
@@ -276,30 +276,26 @@
     }
 
     fun doneClicked(view: View): Unit {
-        val contentValues = ContentValues().apply {
-            var fileName = getFileName(State.uri!!)
-            if (fileName == null) {
-                val d = java.util.Date()
-                fileName = "IMG_%tY%tm%td_%tH%tM%tS.jpg".format(d, d, d, d, d, d)
-            } else {
+        val image = State.bitmap!!
+        binding.progressBar.visibility = ProgressBar.VISIBLE
+        ThreadPools.WORKERS.execute {
+            val contentValues = ContentValues().apply {
+                var fileName = getFileName(State.uri!!)
+                if (fileName == null) {
+                    val d = java.util.Date()
+                    fileName = "IMG_%tY%tm%td_%tH%tM%tS".format(d, d, d, d, d, d)
+                }
                 val dot = fileName.lastIndexOf('.')
-                if (dot == -1) {
-                    fileName = fileName + ".jpg"
-                } else {
-                    val fileExt = fileName.substring(dot).toLowerCase()
-                    if (fileExt !in setOf(".jpg", ".jpeg"))
-                        fileName = fileName.substring(0..dot) + ".jpg"
+                if (dot != -1)
+                    fileName = fileName.substring(0, dot)
+                fileName = "${fileName}_${image.width}x${image.height}.jpg"
+                put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
+                put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
+                    put(MediaStore.MediaColumns.RELATIVE_PATH,
+                            File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).path)
                 }
             }
-            put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
-            put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
-            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
-                put(MediaStore.MediaColumns.RELATIVE_PATH,
-                    File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).getPath())
-            }
-        }
-        binding.progressBar.visibility = ProgressBar.VISIBLE
-        ThreadPools.WORKERS.execute {
             var errorMessage: String? = null
             try {
                 val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
@@ -315,7 +311,7 @@
                                 "jpeg_quality", 85)))
                 Log.d("EditImage", "saving, jpeg_quality = $quality")
                 stream.use {
-                    if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
+                    if (!image.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
                         throw IOException(getString(R.string.error_save_bitmap))
                     }
                 }