changeset 30:aacf7a856b5f

Bug fixes, incl for getting "stuck" when file chooser backed out of.
author David Barts <n5jrn@me.com>
date Wed, 24 Feb 2021 19:14:50 -0800
parents eec88eba58e9
children 0023e6013dd9
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt
diffstat 2 files changed, 24 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Wed Feb 24 18:44:47 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Wed Feb 24 19:14:50 2021 -0800
@@ -94,7 +94,6 @@
     override fun onResume() {
         super.onResume()
 
-        Log.d("EditImage", "intent action = ${intent?.action ?: "(none)"}")
         // Read the URI, die if we can't.
         val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri
         if (imageUri == null) {
@@ -110,15 +109,15 @@
             State.uri = imageUri
             binding.progressBar.visibility = ProgressBar.VISIBLE
             ThreadPools.WORKERS.execute {
-                State.bitmap = contentResolver.openInputStream(imageUri).use {
+                val newBitmap = contentResolver.openInputStream(imageUri).use {
                     BitmapFactory.decodeStream(it)
                 }
                 runOnUiThread {
                     binding.progressBar.visibility = ProgressBar.INVISIBLE
-                    if (State.bitmap == null)
+                    if (newBitmap == null)
                         showFatalError(getString(R.string.error_bad_image))
                     else
-                        setImage(State.bitmap!!)
+                        setImage(newBitmap)
                 }
             }
             return
@@ -137,6 +136,11 @@
         binding.root.invalidate()
     }
 
+    private fun unsetImage(): Unit {
+        State.uri = null
+        State.bitmap = null
+    }
+
     private val CUSTOM = 999998
     private val CANCEL = 999999
 
@@ -270,9 +274,8 @@
         }
     }
 
-     fun cancelClicked(view: View): Unit {
-        State.uri = null
-        State.bitmap = null
+    fun cancelClicked(view: View): Unit {
+        unsetImage()
         finish()
     }
 
@@ -321,9 +324,10 @@
             }
             runOnUiThread {
                 binding.progressBar.visibility = ProgressBar.INVISIBLE
-                if (errorMessage == null)
+                if (errorMessage == null) {
+                    unsetImage()
                     finish()
-                else
+                } else
                     Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show()
             }
         }
--- a/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt	Wed Feb 24 18:44:47 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt	Wed Feb 24 19:14:50 2021 -0800
@@ -23,14 +23,17 @@
 
     override fun onResume() {
         super.onResume()
-        if (showChooser) {
-            Intent(Intent.ACTION_GET_CONTENT).run {
-                type = "image/*"
-                addCategory(Intent.CATEGORY_OPENABLE)
-                startActivityForResult(this, GET_IMAGE)
-            }
-        } else {
+        if (showChooser)
+            doShowChooser()
+        else
             showChooser = true
+    }
+
+    fun doShowChooser(): Unit {
+        Intent(Intent.ACTION_GET_CONTENT).run {
+            type = "image/*"
+            addCategory(Intent.CATEGORY_OPENABLE)
+            startActivityForResult(this, GET_IMAGE)
         }
     }
 
@@ -45,6 +48,7 @@
                 }
             } else {
                 Toast.makeText(applicationContext, "Unable to get image!", Toast.LENGTH_LONG).show()
+                doShowChooser()
             }
         }
     }