How to open file chooser in kivy android webview? - python

I created a webview app using kivy for android. Using following.
from kivy.app import App
from jnius import autoclass
from kivy.clock import Clock
from android.runnable import run_on_ui_thread
from kivy.uix.widget import Widget
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
#run_on_ui_thread
def create_webview(*args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('https://google.com')
class Wv(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.__functionstable__ = {}
Clock.schedule_once(create_webview, 0)
class ServiceApp(App):
def build(self):
return Wv()
Using buildozer I have made apk. But the problem is that when I visit some website and try to upload image using [choose file] button then file chooser window not opeining. I view logcat before click and after click but still not found any log about this.
How can I resovle this issues? How can select image from sdcard and upload it to website opened in webview kivy (android)?
Thanks

Related

What module/s can I use to open a pdf with python that works after I converted it to an apk?

I'm trying to make an android app that opens a pdf file on a click of a button. I am using the modules kivy as gui, subprocess to open a pdf and it worked on my PC but after converting it to an apk with buildozer, the gui works but the button to open a pdf is not working, the button can be seen that it is being pressed but does not do its function. I tried to find the mistake in my code, searching what's wrong and then I found a statement that says the subprocess module does not work on android.
I then used the webbrowser module, same as above it was working on my PC but after it got converted the gui works but when the I click the button that opens a pdf, the app crashes. I would like to see if there's a fix using webbrowser as it looks more promising than looking for another module
Below are the .py files for the one I used subprocess on and then webbrowser.
Using webbrowser
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import webbrowser
file_path = r"file://C:\Users\...\...\...\sample.pdf"
class WelcomeScreen(Screen):
pass
class SecondScreen(Screen):
def openPDF(self):
webbrowser.open_new(file_path)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('design.kv')
class MAINAPP(App):
def build(self):
return kv
if __name__ == '__main__':
MAINAPP().run()
Using Subprocess
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import webbrowser
file_path = "C:\Users\...\...\...\sample.pdf"
class WelcomeScreen(Screen):
pass
class SecondScreen(Screen):
def openPDF(self):
subprocess.Popen((file_path), shell=True)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('design.kv')
class MAINAPP(App):
def build(self):
return kv
if __name__ == '__main__':
MAINAPP().run()

No window opens up in kivy just showing kivy icon

When I run my kivy script,script runs without errors but I see no open window only a kivy icon in the task bar and it is not possible to open window.
This is a screenshot
This my code
from kivy.app import App
from kivy.uix.label import Label
class Test(App):
def build(self):
return Label(text='Hello
World',halign='center')
if __name__=='__main__':
Test().run()

How do I get the kivy Window.softinput_mode = 'below_target' to move the TextInput box above the virtual keyboard?

I'm using python 3.8.6 and kivy 2.1.0.
When the text input box gets focus, the keyboard pops up and covers the TextInput box. I've added the 2 lines below to force the keyboard to be below the target TextInput box, but it does not work as expected.
Window.keyboard_anim_args = {"d":.2,"t":"linear"}
Window.softinput_mode = "below_target"
I've also tried 'pan' and 'resize'. The code runs with no errors, but none of these settings has any effect on the behavior, so I'm sure I'm missing something obvious, but it's not obvious to me:-(. Any help is greatly appreciated. The full code follows:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
Window.keyboard_anim_args = {"d":.2,"t":"linear"}
Window.softinput_mode = "below_target"
#Window.softinput_mode = 'pan'
#Window.softinput_mode = 'resize'
class ClearApp(App):
def build(self):
self.box = BoxLayout(orientation='horizontal', spacing=10)
self.txt = TextInput(hint_text='Write here',
keyboard_mode='auto',
size_hint=(.5,.1))
self.btn = Button(text='Clear All',
on_press=self.clearText, size_hint=(.1,.1))
self.box.add_widget(self.txt)
self.box.add_widget(self.btn)
return self.box
def clearText(self, instance):
self.txt.text = ''
'''
if Config:
_is_desktop = Config.getboolean('kivy', 'desktop')
_keyboard_mode = Config.get('kivy', 'keyboard_mode')
if _is_desktop:
Config.set('kivy', 'keyboard_mode','system')
Config.write()
else:
Config.set('kivy', 'keyboard_mode','systemanddock')
Config.write()
'''
Config.set('kivy', 'keyboard_mode','dock')
Config.write()
ClearApp().run()
Use import Config before import App
which help you to focus and show your keyboard in the UI
for eg.
from kivy.config import Config
from kivy.app import App
use these these line in your code
This function is available only in the master kivy version. so in your buildozer.spec file in the requirements put kivy==master when you are adding kivy to the requirements

How to get a Kivy app on Android to access the Internet

I have a simple Kivy app to try out a clickable link opening a Web site. The code is:
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label
import webbrowser
class TestApp(App):
def build(self):
ref = '[ref=https://kivy.org/doc/stable/]' + 'Kivy documentation' + '[/ref]'
link = Label(text=ref, markup=True, on_ref_press=self.OpenLink)
return link
def OpenLink(self, label, ref):
webbrowser.open(ref)
if __name__ == '__main__':
TestApp().run()
When I run this app in the Kivy Launcher on Android, tapping the link works and opens the url in the browser. However, when I run the compiled app nothing happens when I tap the link.
In the buildozer.spec file I have:
android.permissions = INTERNET
According to all information I have been able to find, this is all I should need. What else am I missing?
I have tried logging the app using adb logcat but I can't see anything relating to the problem.

Kivy Android Save Image

I have a simple program with the goal of saving an image to the Android Gallery.
Here is my code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.image import Image as KVImage
from kivy.core.image import Image
from os import path
class SITApp(App):
def build(self):
return Button(text=path.expanduser('~')+'/DCIM/test.jpg', on_press=self.save)
def save(self, button):
try:
Image('test.png').save(path.expanduser('~')+'/DCIM/test.jpg')
except Exception as e:
button.text = str(e)
SITApp().run()
Right now, the code does not save the image to the specified folder.
Note: The file test.png is a picture in the same directory that looks like the following:
If anyone can help me save this picture in the Gallery of an Android device, it would be extremely appreciated!

Categories