I build a Beeware app, and would lke to find a way to display a .png
picture on my android phone from the phone storage. I understand that the Toga
GUI in Beeware
does not support neither ImageView
nor WebView
on android
, and I could not find any third party module
compatible with Beeware
. Is there any workaround to this? Any suggestion of hint would be much appreciated.I attach below the relevant code that worked in windows but not in android, and the exception that caused the crash
UPDATE: I corrected following the answer by mhsmith
below, and it WORKS GREAT! Many thanks. I updated the code below to reflect the correct script
import toga
from toga import App, Box, MainWindow
from toga import ImageView, Image
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, BOTTOM, TOP, LEFT
with open('/storage/emulated/0/documents/my_picture.bmp',"rb") as f:
image_data = f.read()
image_bytes = bytes(image_data)
class HelloWorld(toga.App):
def startup(self):
main_box = toga.Box(style=Pack(direction=COLUMN))
self.myImage = toga.images.Image(data=image_bytes)
self.img=toga.ImageView(image=self.myImage)
main_box.add(self.img)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def main():
return HelloWorld()
As shown here, ImageView and WebView on Android have a white dot, which means they are supported, though there may be some bugs in the current version.
In this case, your error is caused by the type of the image
argument. Future versions of Toga will accept a string here, but the current stable version requires an Image
object.