# HG changeset patch # User David Barts # Date 1612197012 28800 # Node ID f26f61a8a9ad75f9d77b295192ae058024ae549e # Parent 13935000c2d99f2812cc3b34f03ff13e4af678a8 Make the EditImage activity stateful. diff -r 13935000c2d9 -r f26f61a8a9ad app/src/main/AndroidManifest.xml --- a/app/src/main/AndroidManifest.xml Sun Jan 31 08:21:24 2021 -0800 +++ b/app/src/main/AndroidManifest.xml Mon Feb 01 08:30:12 2021 -0800 @@ -12,7 +12,6 @@ - @@ -20,7 +19,6 @@ - diff -r 13935000c2d9 -r f26f61a8a9ad app/src/main/java/com/bartsent/simpleresizer/EditImage.kt --- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Sun Jan 31 08:21:24 2021 -0800 +++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Mon Feb 01 08:30:12 2021 -0800 @@ -1,10 +1,12 @@ package com.bartsent.simpleresizer +import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.provider.OpenableColumns import android.text.Html import android.text.Spanned import android.view.View @@ -16,6 +18,11 @@ class EditImage : AppCompatActivity() { + object State { + var uri: Uri? = null + var bitmap: Bitmap? = null + } + private lateinit var binding: ActivityEditImageBinding override fun onCreate(savedInstanceState: Bundle?) { @@ -37,24 +44,54 @@ Html.fromHtml(input) } + // Cribbed from: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content + private fun getFileName(uri: Uri): String? { + var result: String? = null + if (uri.scheme == "content") { + contentResolver.query(uri, null, null, null, null).use { cursor -> + if (cursor != null && cursor.moveToFirst()) + result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) + } + } + if (result == null) { + val uriPath = uri.path + result = uriPath?.substring(uriPath.lastIndexOf('/') + 1) + } + return result + } + override fun onResume() { super.onResume() + + // Read the URI, die if we can't. val imageUri = intent?.data if (imageUri == null) { binding.imageStatusReport.text = fromHtml("Error: no URI supplied!") - return; + return } - val imageBitmap = contentResolver.openInputStream(imageUri).use { - BitmapFactory.decodeStream(it) + + // Being stateful stops data loss when the phone gets rotated. + if (imageUri != State.uri) { + State.uri = imageUri + State.bitmap = contentResolver.openInputStream(imageUri).use { + BitmapFactory.decodeStream(it) + } } + if (State.bitmap == null) { + binding.imageStatusReport.text = fromHtml("Error: bad image!") + return + } + val imageBitmap = State.bitmap!! + + // OK, we have the Bitmap; operate on it. binding.imageStatusReport.text = fromHtml( item("Uri", imageUri) + + item("File-Name", getFileName((imageUri))) + item("Byte-Count", imageBitmap.byteCount) + item("Density", imageBitmap.density) + item("Height", imageBitmap.height) + item("Width", imageBitmap.width) + item("Has-Alpha", imageBitmap.hasAlpha()) ) - Toast.makeText(applicationContext, "Exiting.", Toast.LENGTH_LONG).show() // debug } -} \ No newline at end of file +}