comparison android/DWindows.kt @ 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
comparison
equal deleted inserted replaced
2563:56183bef6793 2564:607acfe2c504
51 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener 51 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
52 import com.google.android.material.tabs.TabLayoutMediator 52 import com.google.android.material.tabs.TabLayoutMediator
53 import java.io.File 53 import java.io.File
54 import java.io.FileInputStream 54 import java.io.FileInputStream
55 import java.io.FileNotFoundException 55 import java.io.FileNotFoundException
56 import java.io.IOException
56 import java.util.* 57 import java.util.*
57 import java.util.concurrent.locks.ReentrantLock 58 import java.util.concurrent.locks.ReentrantLock
58 59
59 60
60 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() { 61 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() {
1325 fun bitmapButtonNewFromFile(text: String, cid: Int, filename: String): ImageButton? { 1326 fun bitmapButtonNewFromFile(text: String, cid: Int, filename: String): ImageButton? {
1326 var button: ImageButton? = null 1327 var button: ImageButton? = null
1327 waitOnUiThread { 1328 waitOnUiThread {
1328 button = ImageButton(this) 1329 button = ImageButton(this)
1329 var dataArrayMap = SimpleArrayMap<String, Long>() 1330 var dataArrayMap = SimpleArrayMap<String, Long>()
1331 var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
1330 1332
1331 button!!.tag = dataArrayMap 1333 button!!.tag = dataArrayMap
1332 button!!.id = cid 1334 button!!.id = cid
1333 button!!.setOnClickListener { 1335 button!!.setOnClickListener {
1334 eventHandlerSimple(button!!, 8) 1336 eventHandlerSimple(button!!, 8)
1335 } 1337 }
1336 // Try to load the image, and protect against exceptions 1338
1337 try { 1339 for (ext in exts) {
1338 val f = File(filename) 1340 // Try to load the image, and protect against exceptions
1339 val b = BitmapFactory.decodeStream(FileInputStream(f)) 1341 try {
1340 button!!.setImageBitmap(b) 1342 val f = this.assets.open(filename + ext)
1341 } 1343 val b = BitmapFactory.decodeStream(f)
1342 catch (e: FileNotFoundException) 1344
1343 { 1345 if(b != null) {
1346 button!!.setImageBitmap(b)
1347 break
1348 }
1349 } catch (e: IOException) {
1350 }
1344 } 1351 }
1345 } 1352 }
1346 return button 1353 return button
1347 } 1354 }
1348 1355
2600 val imageview = window 2607 val imageview = window
2601 2608
2602 imageview.setImageResource(resID) 2609 imageview.setImageResource(resID)
2603 } 2610 }
2604 } else if(filename != null) { 2611 } else if(filename != null) {
2605 // Try to load the image, and protect against exceptions 2612 var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
2606 try { 2613
2607 val f = File(filename) 2614 for (ext in exts) {
2608 val b = BitmapFactory.decodeStream(FileInputStream(f)) 2615 // Try to load the image, and protect against exceptions
2609 if(window is ImageButton) { 2616 try {
2610 val button = window 2617 val f = this.assets.open(filename + ext)
2611 2618 val b = BitmapFactory.decodeStream(f)
2612 button.setImageBitmap(b) 2619
2613 } else if(window is ImageView) { 2620 if(b != null) {
2614 val imageview = window 2621 if (window is ImageButton) {
2615 2622 val button = window
2616 imageview.setImageBitmap(b) 2623
2617 } 2624 button.setImageBitmap(b)
2618 } catch (e: FileNotFoundException) { 2625 } else if (window is ImageView) {
2626 val imageview = window
2627
2628 imageview.setImageBitmap(b)
2629 }
2630 break
2631 }
2632 } catch (e: IOException) {
2633 }
2619 } 2634 }
2620 } 2635 }
2621 } 2636 }
2622 } 2637 }
2623 2638
2656 2671
2657 waitOnUiThread { 2672 waitOnUiThread {
2658 if(resID != 0) { 2673 if(resID != 0) {
2659 icon = ResourcesCompat.getDrawable(resources, resID, null); 2674 icon = ResourcesCompat.getDrawable(resources, resID, null);
2660 } else if(filename != null) { 2675 } else if(filename != null) {
2661 // Try to load the image, and protect against exceptions 2676 var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
2662 try { 2677
2663 icon = Drawable.createFromPath(filename) 2678 for (ext in exts) {
2664 } catch (e: FileNotFoundException) { 2679 // Try to load the image, and protect against exceptions
2680 try {
2681 val f = this.assets.open(filename + ext)
2682 icon = Drawable.createFromStream(f, null)
2683 } catch (e: IOException) {
2684 }
2685 if(icon != null) {
2686 break
2687 }
2688
2665 } 2689 }
2666 } else if(data != null) { 2690 } else if(data != null) {
2667 icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length)) 2691 icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length))
2668 } 2692 }
2669 } 2693 }
2678 if(width > 0 && height > 0) { 2702 if(width > 0 && height > 0) {
2679 pixmap = Bitmap.createBitmap(null, width, height, Bitmap.Config.ARGB_8888) 2703 pixmap = Bitmap.createBitmap(null, width, height, Bitmap.Config.ARGB_8888)
2680 } else if(resID != 0) { 2704 } else if(resID != 0) {
2681 pixmap = BitmapFactory.decodeResource(resources, resID); 2705 pixmap = BitmapFactory.decodeResource(resources, resID);
2682 } else if(filename != null) { 2706 } else if(filename != null) {
2683 // Try to load the image, and protect against exceptions 2707 var exts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
2684 try { 2708
2685 val f = File(filename) 2709 for (ext in exts) {
2686 pixmap = BitmapFactory.decodeStream(FileInputStream(f)) 2710 // Try to load the image, and protect against exceptions
2687 } catch (e: FileNotFoundException) { 2711 try {
2712 val f = this.assets.open(filename + ext)
2713 pixmap = BitmapFactory.decodeStream(f)
2714 } catch (e: IOException) {
2715 }
2716 if(pixmap != null) {
2717 break
2718 }
2688 } 2719 }
2689 } else if(data != null) { 2720 } else if(data != null) {
2690 pixmap = BitmapFactory.decodeByteArray(data, 0, length) 2721 pixmap = BitmapFactory.decodeByteArray(data, 0, length)
2691 } 2722 }
2692 } 2723 }