changeset 1:f26f61a8a9ad

Make the EditImage activity stateful.
author David Barts <n5jrn@me.com>
date Mon, 01 Feb 2021 08:30:12 -0800
parents 13935000c2d9
children 06825e49f7aa
files app/src/main/AndroidManifest.xml app/src/main/java/com/bartsent/simpleresizer/EditImage.kt
diffstat 2 files changed, 42 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
         <activity android:name=".EditImage">
             <intent-filter>
                 <action android:name="android.intent.action.SEND" />
-                <action android:name="android.intent.action.SEND_MULTIPLE" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <data android:mimeType="image/*" />
             </intent-filter>
@@ -20,7 +19,6 @@
         <activity android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
-
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
--- 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("<b>Error:</b> 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("<b>Error:</b> 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
+}