comparison android/DWindows.kt @ 2652:42151fd096d4

Android: Switch to loading resource images like we do on iOS. Instead of using the Android resource ID, which is created automatically... Try to load the resource ID as the filename from the assets folder. If the Resource ID is outside the 1 to 65535 range, try Android resource loading, in case it actually is a native Android resource ID.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 14 Sep 2021 11:12:45 +0000
parents 7700c8022af3
children 297456a381e4
comparison
equal deleted inserted replaced
2651:7700c8022af3 2652:42151fd096d4
3002 } 3002 }
3003 } 3003 }
3004 } 3004 }
3005 } 3005 }
3006 3006
3007 fun iconNew(filename: String?, data: ByteArray?, length: Int, resID: Int): Drawable? 3007 fun iconNew(file: String?, data: ByteArray?, length: Int, resID: Int): Drawable?
3008 { 3008 {
3009 var icon: Drawable? = null 3009 var icon: Drawable? = null
3010 3010
3011 waitOnUiThread { 3011 waitOnUiThread {
3012 if(resID != 0) { 3012 var filename: String? = file
3013 try { 3013
3014 icon = ResourcesCompat.getDrawable(resources, resID, null) 3014 // Handle Dynamic Windows resource IDs
3015 } catch(e: Resources.NotFoundException) { 3015 if(resID > 0 && resID < 65536) {
3016 } 3016 filename = resID.toString()
3017 } else if(filename != null) { 3017 }
3018 // Handle filename or DW resource IDs
3019 // these will be located in the assets folder
3020 if(filename != null) {
3018 val exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif") 3021 val exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
3019 3022
3020 for (ext in exts) { 3023 for (ext in exts) {
3021 // Try to load the image, and protect against exceptions 3024 // Try to load the image, and protect against exceptions
3022 try { 3025 try {
3023 val f = this.assets.open(filename + ext) 3026 val f = this.assets.open(filename + ext)
3024 icon = Drawable.createFromStream(f, null) 3027 icon = Drawable.createFromStream(f, null)
3025 } catch (e: IOException) { 3028 } catch (e: IOException) {
3026 } 3029 }
3027 if(icon != null) { 3030 if (icon != null) {
3028 break 3031 break
3029 } 3032 }
3030 } 3033 }
3034 // Handle Android resource IDs
3035 } else if(resID != 0) {
3036 try {
3037 icon = ResourcesCompat.getDrawable(resources, resID, null)
3038 } catch (e: Resources.NotFoundException) {
3039 }
3040 // Handle bitmap data
3031 } else if(data != null) { 3041 } else if(data != null) {
3032 icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length)) 3042 icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length))
3033 } 3043 }
3034 } 3044 }
3035 return icon 3045 return icon