changeset 2564:607acfe2c504

Android: The *_from_file() functions now check the assets folder. Also try all the loadable file extensions that I am aware of for images. Not sure if I should try absolute paths too or just leave it only assets. Create an "assets" folder under main and put file.png and folder.png in. Create a subfolder "image" and put test.png in. This will create an app with the required images.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 19 May 2021 08:13:13 +0000
parents 56183bef6793
children 5463801a888f
files android/DWindows.kt
diffstat 1 files changed, 61 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Wed May 19 01:08:59 2021 +0000
+++ b/android/DWindows.kt	Wed May 19 08:13:13 2021 +0000
@@ -53,6 +53,7 @@
 import java.io.File
 import java.io.FileInputStream
 import java.io.FileNotFoundException
+import java.io.IOException
 import java.util.*
 import java.util.concurrent.locks.ReentrantLock
 
@@ -1327,20 +1328,26 @@
         waitOnUiThread {
             button = ImageButton(this)
             var dataArrayMap = SimpleArrayMap<String, Long>()
+            var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
 
             button!!.tag = dataArrayMap
             button!!.id = cid
             button!!.setOnClickListener {
                 eventHandlerSimple(button!!, 8)
             }
-            // Try to load the image, and protect against exceptions
-            try {
-                val f = File(filename)
-                val b = BitmapFactory.decodeStream(FileInputStream(f))
-                button!!.setImageBitmap(b)
-            }
-            catch (e: FileNotFoundException)
-            {
+
+            for (ext in exts) {
+                // Try to load the image, and protect against exceptions
+                try {
+                    val f = this.assets.open(filename + ext)
+                    val b = BitmapFactory.decodeStream(f)
+
+                    if(b != null) {
+                        button!!.setImageBitmap(b)
+                        break
+                    }
+                } catch (e: IOException) {
+                }
             }
         }
         return button
@@ -2602,20 +2609,28 @@
                     imageview.setImageResource(resID)
                 }
             } else if(filename != null) {
-                // Try to load the image, and protect against exceptions
-                try {
-                    val f = File(filename)
-                    val b = BitmapFactory.decodeStream(FileInputStream(f))
-                    if(window is ImageButton) {
-                        val button = window
-
-                        button.setImageBitmap(b)
-                    } else if(window is ImageView) {
-                        val imageview = window
-
-                        imageview.setImageBitmap(b)
+                var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
+
+                for (ext in exts) {
+                    // Try to load the image, and protect against exceptions
+                    try {
+                        val f = this.assets.open(filename + ext)
+                        val b = BitmapFactory.decodeStream(f)
+
+                        if(b != null) {
+                            if (window is ImageButton) {
+                                val button = window
+
+                                button.setImageBitmap(b)
+                            } else if (window is ImageView) {
+                                val imageview = window
+
+                                imageview.setImageBitmap(b)
+                            }
+                            break
+                        }
+                    } catch (e: IOException) {
                     }
-                } catch (e: FileNotFoundException) {
                 }
             }
         }
@@ -2658,10 +2673,19 @@
             if(resID != 0) {
                 icon = ResourcesCompat.getDrawable(resources, resID, null);
             } else if(filename != null) {
-                // Try to load the image, and protect against exceptions
-                try {
-                    icon = Drawable.createFromPath(filename)
-                } catch (e: FileNotFoundException) {
+                var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
+
+                for (ext in exts) {
+                    // Try to load the image, and protect against exceptions
+                    try {
+                        val f = this.assets.open(filename + ext)
+                        icon = Drawable.createFromStream(f, null)
+                    } catch (e: IOException) {
+                    }
+                    if(icon != null) {
+                        break
+                    }
+
                 }
             } else if(data != null) {
                 icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length))
@@ -2680,11 +2704,18 @@
             } else if(resID != 0) {
                 pixmap = BitmapFactory.decodeResource(resources, resID);
             } else if(filename != null) {
-                // Try to load the image, and protect against exceptions
-                try {
-                    val f = File(filename)
-                    pixmap = BitmapFactory.decodeStream(FileInputStream(f))
-                } catch (e: FileNotFoundException) {
+                var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
+
+                for (ext in exts) {
+                    // Try to load the image, and protect against exceptions
+                    try {
+                        val f = this.assets.open(filename + ext)
+                        pixmap = BitmapFactory.decodeStream(f)
+                    } catch (e: IOException) {
+                    }
+                    if(pixmap != null) {
+                        break
+                    }
                 }
             } else if(data != null) {
                 pixmap = BitmapFactory.decodeByteArray(data, 0, length)