comparison app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 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 be08576794af
comparison
equal deleted inserted replaced
29:eec88eba58e9 30:aacf7a856b5f
92 } 92 }
93 93
94 override fun onResume() { 94 override fun onResume() {
95 super.onResume() 95 super.onResume()
96 96
97 Log.d("EditImage", "intent action = ${intent?.action ?: "(none)"}")
98 // Read the URI, die if we can't. 97 // Read the URI, die if we can't.
99 val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri 98 val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri
100 if (imageUri == null) { 99 if (imageUri == null) {
101 if (State.bitmap == null) 100 if (State.bitmap == null)
102 showFatalError(getString(R.string.error_no_uri)) 101 showFatalError(getString(R.string.error_no_uri))
108 // User has opened a new image. 107 // User has opened a new image.
109 if (imageUri != State.uri) { 108 if (imageUri != State.uri) {
110 State.uri = imageUri 109 State.uri = imageUri
111 binding.progressBar.visibility = ProgressBar.VISIBLE 110 binding.progressBar.visibility = ProgressBar.VISIBLE
112 ThreadPools.WORKERS.execute { 111 ThreadPools.WORKERS.execute {
113 State.bitmap = contentResolver.openInputStream(imageUri).use { 112 val newBitmap = contentResolver.openInputStream(imageUri).use {
114 BitmapFactory.decodeStream(it) 113 BitmapFactory.decodeStream(it)
115 } 114 }
116 runOnUiThread { 115 runOnUiThread {
117 binding.progressBar.visibility = ProgressBar.INVISIBLE 116 binding.progressBar.visibility = ProgressBar.INVISIBLE
118 if (State.bitmap == null) 117 if (newBitmap == null)
119 showFatalError(getString(R.string.error_bad_image)) 118 showFatalError(getString(R.string.error_bad_image))
120 else 119 else
121 setImage(State.bitmap!!) 120 setImage(newBitmap)
122 } 121 }
123 } 122 }
124 return 123 return
125 } 124 }
126 125
133 private fun setImage(image: Bitmap): Unit { 132 private fun setImage(image: Bitmap): Unit {
134 binding.imageSize.text = getString(R.string.image_size_text, image.width, image.height) 133 binding.imageSize.text = getString(R.string.image_size_text, image.width, image.height)
135 binding.image.setImageBitmap(image) 134 binding.image.setImageBitmap(image)
136 State.bitmap = image 135 State.bitmap = image
137 binding.root.invalidate() 136 binding.root.invalidate()
137 }
138
139 private fun unsetImage(): Unit {
140 State.uri = null
141 State.bitmap = null
138 } 142 }
139 143
140 private val CUSTOM = 999998 144 private val CUSTOM = 999998
141 private val CANCEL = 999999 145 private val CANCEL = 999999
142 146
268 setImage(newBitmap) 272 setImage(newBitmap)
269 } 273 }
270 } 274 }
271 } 275 }
272 276
273 fun cancelClicked(view: View): Unit { 277 fun cancelClicked(view: View): Unit {
274 State.uri = null 278 unsetImage()
275 State.bitmap = null
276 finish() 279 finish()
277 } 280 }
278 281
279 fun doneClicked(view: View): Unit { 282 fun doneClicked(view: View): Unit {
280 val image = State.bitmap!! 283 val image = State.bitmap!!
319 } catch (ioe: IOException) { 322 } catch (ioe: IOException) {
320 errorMessage = ioe.message ?: getString(R.string.error_io) 323 errorMessage = ioe.message ?: getString(R.string.error_io)
321 } 324 }
322 runOnUiThread { 325 runOnUiThread {
323 binding.progressBar.visibility = ProgressBar.INVISIBLE 326 binding.progressBar.visibility = ProgressBar.INVISIBLE
324 if (errorMessage == null) 327 if (errorMessage == null) {
328 unsetImage()
325 finish() 329 finish()
326 else 330 } else
327 Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show() 331 Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show()
328 } 332 }
329 } 333 }
330 } 334 }
331 } 335 }