Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 36:70f1d11d53ad
Attempt to make it work on Android 9 and earlier.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 19 Mar 2021 12:49:13 -0700 |
parents | bead5d7e8c69 |
children | 0dbd924cb5e8 |
comparison
equal
deleted
inserted
replaced
35:6607f675a5f7 | 36:70f1d11d53ad |
---|---|
1 package com.bartsent.simpleresizer | 1 package com.bartsent.simpleresizer |
2 | 2 |
3 import android.Manifest | |
3 import android.content.ContentValues | 4 import android.content.ContentValues |
4 import android.content.Intent | 5 import android.content.Intent |
6 import android.content.pm.PackageManager | |
5 import android.graphics.Bitmap | 7 import android.graphics.Bitmap |
6 import android.graphics.BitmapFactory | 8 import android.graphics.BitmapFactory |
7 import android.graphics.Canvas | 9 import android.graphics.Canvas |
8 import android.graphics.Matrix | 10 import android.graphics.Matrix |
9 import android.net.Uri | 11 import android.net.Uri |
96 } | 98 } |
97 it.create() | 99 it.create() |
98 }.show() | 100 }.show() |
99 } | 101 } |
100 | 102 |
103 fun showError(message: String): Unit { | |
104 AlertDialog.Builder(this).also { | |
105 it.setMessage(message) | |
106 it.setNeutralButton(R.string.ok_text) { dialog, _ -> | |
107 dialog.dismiss() | |
108 } | |
109 it.create() | |
110 }.show() | |
111 } | |
112 | |
101 override fun onResume() { | 113 override fun onResume() { |
102 super.onResume() | 114 super.onResume() |
103 | 115 |
104 // Read the URI, die if we can't. | 116 // Read the URI, die if we can't. |
105 val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri | 117 val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri |
297 fun cancelClicked(view: View): Unit { | 309 fun cancelClicked(view: View): Unit { |
298 unsetImage() | 310 unsetImage() |
299 finish() | 311 finish() |
300 } | 312 } |
301 | 313 |
302 fun doneClicked(view: View): Unit { | 314 private val REQUEST_WRITE_EXTERNAL = 42 |
315 | |
316 override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray): Unit { | |
317 if (requestCode != REQUEST_WRITE_EXTERNAL) { | |
318 Log.e("EditImage", "unexpected request code in onRequestPermissionsResult!") | |
319 return | |
320 } | |
321 if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { | |
322 doneClicked(null) | |
323 } else { | |
324 showError(getString(R.string.error_unable_no_permissions)) | |
325 } | |
326 } | |
327 | |
328 fun doneClicked(view: View?): Unit { | |
329 // If we need a permission, request it and bail. We will be called again | |
330 // (with the permission) if it is granted. | |
331 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) { | |
332 if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { | |
333 requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_WRITE_EXTERNAL) | |
334 return | |
335 } | |
336 } | |
337 | |
338 // If we get here, we have permission to save (if we need it). | |
303 val image = viewModel.bitmap!! | 339 val image = viewModel.bitmap!! |
304 binding.progressBar.visibility = ProgressBar.VISIBLE | 340 binding.progressBar.visibility = ProgressBar.VISIBLE |
305 ThreadPools.WORKERS.execute { | 341 ThreadPools.WORKERS.execute { |
306 val contentValues = ContentValues().apply { | 342 val contentValues = ContentValues().apply { |
307 var fileName = getFileName(viewModel.uri!!) | 343 var fileName = getFileName(viewModel.uri!!) |
339 throw IOException(getString(R.string.error_save_bitmap)) | 375 throw IOException(getString(R.string.error_save_bitmap)) |
340 } | 376 } |
341 } | 377 } |
342 } catch (ioe: IOException) { | 378 } catch (ioe: IOException) { |
343 errorMessage = ioe.message ?: getString(R.string.error_io) | 379 errorMessage = ioe.message ?: getString(R.string.error_io) |
380 } catch (se: SecurityException) { | |
381 errorMessage = se.message ?: getString(R.string.error_security) | |
382 } catch (e: Exception) { | |
383 Log.e("EditImage", "unexpected exception when saving!", e) | |
384 errorMessage = e.message ?: getString(R.string.error_unexpected, e::class.qualifiedName) | |
344 } | 385 } |
345 runOnUiThread { | 386 runOnUiThread { |
346 binding.progressBar.visibility = ProgressBar.INVISIBLE | 387 binding.progressBar.visibility = ProgressBar.INVISIBLE |
347 if (errorMessage == null) { | 388 if (errorMessage == null) { |
348 unsetImage() | 389 unsetImage() |
349 finish() | 390 finish() |
350 } else | 391 } else { |
351 Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show() | 392 showError(errorMessage) |
393 } | |
352 } | 394 } |
353 } | 395 } |
354 } | 396 } |
355 } | 397 } |