· 5 years ago · Jul 13, 2020, 03:58 PM
1from kivymd.app import MDApp
2from kivy.lang import Builder
3from kivy.utils import platform
4from kivy.clock import mainthread
5
6
7if platform == 'android':
8 from jnius import autoclass, cast
9 from android.runnable import run_on_ui_thread
10 from android import activity
11
12 Intent = autoclass('android.content.Intent')
13 Activity = autoclass('android.app.Activity')
14 PythonActivity = autoclass('org.kivy.android.PythonActivity')
15 File = autoclass('java.io.File')
16 Paths = autoclass('java.nio.file.Paths')
17else:
18 def run_on_ui_thread(*args):
19 return
20
21KV = """
22#: import p kivy.utils.platform
23
24Screen:
25 name: 'first'
26 MDRaisedButton:
27 id: btn
28 center: root.center
29 text: 'open'
30 on_release:
31 app.get_picture() if p == 'android' else print('compile to apk')
32 MDLabel:
33 id: lbl
34 text: 'test'
35 halign: 'center'
36 vlign: 'middle'
37 pos_hint: {'center_y': .3}
38"""
39
40
41class GalleryApp(MDApp):
42
43 PICK_IMAGE = 1
44
45 def build(self):
46 if platform == 'android':
47 activity.bind(on_activity_result=self.activity_result)
48 return Builder.load_string(KV)
49
50 @run_on_ui_thread
51 def get_picture(self):
52 photoPickerIntent = Intent(Intent.ACTION_PICK)
53 photoPickerIntent.setType("image/*")
54 currentActivity = cast(
55 'android.app.Activity', PythonActivity.mActivity)
56 currentActivity.startActivityForResult(
57 photoPickerIntent, self.PICK_IMAGE)
58
59 @mainthread
60 def activity_result(self, requestCode, resultCode, data):
61 if requestCode == self.PICK_IMAGE and Activity.RESULT_OK:
62 uri = data.getData()
63 self.root.ids.lbl.text = str(uri)
64 # you do the rest!
65
66
67if __name__ == '__main__':
68 app = GalleryApp()
69 app.run()
70
71
72
73################
74# buildozer #
75
76[app]
77
78# (str) Title of your application
79title = Test
80
81# (str) Package name
82package.name = test
83
84# (str) Package domain (needed for android/ios packaging)
85package.domain = org.test
86
87# (str) Source code where the main.py live
88source.dir = .
89
90# (list) Source files to include (let empty to include all the files)
91source.include_exts = py,png,jpg,kv,atlas
92
93# (list) List of inclusions using pattern matching
94#source.include_patterns = assets/*,images/*.png
95
96# (list) Source files to exclude (let empty to not exclude anything)
97source.exclude_exts = spec
98
99# (list) List of directory to exclude (let empty to not exclude anything)
100source.exclude_dirs = tests,bin
101
102# (list) List of exclusions using pattern matching
103#source.exclude_patterns = license,images/*/*.jpg
104
105# (str) Application versioning (method 1)
106version = 0.1
107
108# (str) Application versioning (method 2)
109# version.regex = __version__ = ['"](.*)['"]
110# version.filename = %(source.dir)s/main.py
111
112# (list) Application requirements
113# comma separated e.g. requirements = sqlite3,kivy
114requirements = python3,kivy,git+https://github.com/HeaTTheatR/KivyMD.git
115
116# (str) Custom source folders for requirements
117# Sets custom source for any requirements with recipes
118# requirements.source.kivy = ../../kivy
119
120# (list) Garden requirements
121#garden_requirements =
122
123# (str) Presplash of the application
124#presplash.filename = %(source.dir)s/data/presplash.png
125
126# (str) Icon of the application
127#icon.filename = %(source.dir)s/data/icon.png
128
129# (str) Supported orientation (one of landscape, sensorLandscape, portrait or all)
130orientation = portrait
131
132# (list) List of service to declare
133#services = NAME:ENTRYPOINT_TO_PY,NAME2:ENTRYPOINT2_TO_PY
134
135#
136# OSX Specific
137#
138
139#
140# author = © Copyright Info
141
142# change the major version of python used by the app
143osx.python_version = 3
144
145# Kivy version to use
146osx.kivy_version = 1.9.1
147
148#
149# Android specific
150#
151
152# (bool) Indicate if the application should be fullscreen or not
153fullscreen = 0
154
155# (string) Presplash background color (for new android toolchain)
156# Supported formats are: #RRGGBB #AARRGGBB or one of the following names:
157# red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray,
158# darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy,
159# olive, purple, silver, teal.
160#android.presplash_color = #FFFFFF
161
162# (list) Permissions
163android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE,WAKE_LOCK
164
165# (int) Target Android API, should be as high as possible.
166android.api = 29
167
168# (int) Minimum API your APK will support.
169#android.minapi = 21
170
171# (int) Android SDK version to use
172#android.sdk = 20
173
174# (str) Android NDK version to use
175#android.ndk = 19b
176
177# (int) Android NDK API to use. This is the minimum API your app will support, it should usually match android.minapi.
178#android.ndk_api = 21
179
180# (bool) Use --private data storage (True) or --dir public storage (False)
181#android.private_storage = True
182
183# (str) Android NDK directory (if empty, it will be automatically downloaded.)
184#android.ndk_path =
185
186# (str) Android SDK directory (if empty, it will be automatically downloaded.)
187#android.sdk_path =
188
189# (str) ANT directory (if empty, it will be automatically downloaded.)
190#android.ant_path =
191
192# (bool) If True, then skip trying to update the Android sdk
193# This can be useful to avoid excess Internet downloads or save time
194# when an update is due and you just want to test/build your package
195android.skip_update = True
196
197# (bool) If True, then automatically accept SDK license
198# agreements. This is intended for automation only. If set to False,
199# the default, you will be shown the license when first running
200# buildozer.
201# android.accept_sdk_license = False
202
203# (str) Android entry point, default is ok for Kivy-based app
204#android.entrypoint = org.renpy.android.PythonActivity
205
206# (str) Android app theme, default is ok for Kivy-based app
207# android.apptheme = "@android:style/Theme.NoTitleBar"
208
209# (list) Pattern to whitelist for the whole project
210#android.whitelist =
211
212# (str) Path to a custom whitelist file
213#android.whitelist_src =
214
215# (str) Path to a custom blacklist file
216#android.blacklist_src =
217
218# (list) List of Java .jar files to add to the libs so that pyjnius can access
219# their classes. Don't add jars that you do not need, since extra jars can slow
220# down the build process. Allows wildcards matching, for example:
221# OUYA-ODK/libs/*.jar
222#android.add_jars = foo.jar,bar.jar,path/to/more/*.jar
223
224# (list) List of Java files to add to the android project (can be java or a
225# directory containing the files)
226#android.add_src =
227
228# (list) Android AAR archives to add (currently works only with sdl2_gradle
229# bootstrap)
230#android.add_aars =
231
232# (list) Gradle dependencies to add (currently works only with sdl2_gradle
233# bootstrap)
234#android.gradle_dependencies =
235
236# (list) add java compile options
237# this can for example be necessary when importing certain java libraries using the 'android.gradle_dependencies' option
238# see https://developer.android.com/studio/write/java8-support for further information
239# android.add_compile_options = "sourceCompatibility = 1.8", "targetCompatibility = 1.8"
240
241# (list) Gradle repositories to add {can be necessary for some android.gradle_dependencies}
242# please enclose in double quotes
243# e.g. android.gradle_repositories = "maven { url 'https://kotlin.bintray.com/ktor' }"
244#android.add_gradle_repositories =
245
246# (list) packaging options to add
247# see https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
248# can be necessary to solve conflicts in gradle_dependencies
249# please enclose in double quotes
250# e.g. android.add_packaging_options = "exclude 'META-INF/common.kotlin_module'", "exclude 'META-INF/*.kotlin_module'"
251#android.add_gradle_repositories =
252
253# (list) Java classes to add as activities to the manifest.
254#android.add_activities = com.example.ExampleActivity
255
256# (str) OUYA Console category. Should be one of GAME or APP
257# If you leave this blank, OUYA support will not be enabled
258#android.ouya.category = GAME
259
260# (str) Filename of OUYA Console icon. It must be a 732x412 png image.
261#android.ouya.icon.filename = %(source.dir)s/data/ouya_icon.png
262
263# (str) XML file to include as an intent filters in <activity> tag
264#android.manifest.intent_filters =
265
266# (str) launchMode to set for the main activity
267#android.manifest.launch_mode = standard
268
269# (list) Android additional libraries to copy into libs/armeabi
270#android.add_libs_armeabi = libs/android/*.so
271#android.add_libs_armeabi_v7a = libs/android-v7/*.so
272#android.add_libs_arm64_v8a = libs/android-v8/*.so
273#android.add_libs_x86 = libs/android-x86/*.so
274#android.add_libs_mips = libs/android-mips/*.so
275
276# (bool) Indicate whether the screen should stay on
277# Don't forget to add the WAKE_LOCK permission if you set this to True
278android.wakelock = True
279
280# (list) Android application meta-data to set (key=value format)
281#android.meta_data =
282
283# (list) Android library project to add (will be added in the
284# project.properties automatically.)
285#android.library_references =
286
287# (list) Android shared libraries which will be added to AndroidManifest.xml using <uses-library> tag
288#android.uses_library =
289
290# (str) Android logcat filters to use
291android.logcat_filters = *:S python:D
292
293# (bool) Copy library instead of making a libpymodules.so
294#android.copy_libs = 1
295
296# (str) The Android arch to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64
297android.arch = armeabi-v7a
298
299# (int) overrides automatic versionCode computation (used in build.gradle)
300# this is not the same as app version and should only be edited if you know what you're doing
301# android.numeric_version = 1
302
303#
304# Python for android (p4a) specific
305#
306
307# (str) python-for-android fork to use, defaults to upstream (kivy)
308#p4a.fork = kivy
309
310# (str) python-for-android branch to use, defaults to master
311#p4a.branch = master
312
313# (str) python-for-android git clone directory (if empty, it will be automatically cloned from github)
314#p4a.source_dir =
315
316# (str) The directory in which python-for-android should look for your own build recipes (if any)
317#p4a.local_recipes =
318
319# (str) Filename to the hook for p4a
320#p4a.hook =
321
322# (str) Bootstrap to use for android builds
323# p4a.bootstrap = sdl2
324
325# (int) port number to specify an explicit --port= p4a argument (eg for bootstrap flask)
326#p4a.port =
327
328
329#
330# iOS specific
331#
332
333# (str) Path to a custom kivy-ios folder
334#ios.kivy_ios_dir = ../kivy-ios
335# Alternately, specify the URL and branch of a git checkout:
336ios.kivy_ios_url = https://github.com/kivy/kivy-ios
337ios.kivy_ios_branch = master
338
339# Another platform dependency: ios-deploy
340# Uncomment to use a custom checkout
341#ios.ios_deploy_dir = ../ios_deploy
342# Or specify URL and branch
343ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
344ios.ios_deploy_branch = 1.7.0
345
346# (str) Name of the certificate to use for signing the debug version
347# Get a list of available identities: buildozer ios list_identities
348#ios.codesign.debug = "iPhone Developer: <lastname> <firstname> (<hexstring>)"
349
350# (str) Name of the certificate to use for signing the release version
351#ios.codesign.release = %(ios.codesign.debug)s
352
353
354[buildozer]
355
356# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
357log_level = 2
358
359# (int) Display warning if buildozer is run as root (0 = False, 1 = True)
360warn_on_root = 1
361
362# (str) Path to build artifact storage, absolute or relative to spec file
363# build_dir = ./.buildozer
364
365# (str) Path to build output (i.e. .apk, .ipa) storage
366# bin_dir = ./bin
367
368# -----------------------------------------------------------------------------
369# List as sections
370#
371# You can define all the "list" as [section:key].
372# Each line will be considered as a option to the list.
373# Let's take [app] / source.exclude_patterns.
374# Instead of doing:
375#
376#[app]
377#source.exclude_patterns = license,data/audio/*.wav,data/images/original/*
378#
379# This can be translated into:
380#
381#[app:source.exclude_patterns]
382#license
383#data/audio/*.wav
384#data/images/original/*
385#
386
387
388# -----------------------------------------------------------------------------
389# Profiles
390#
391# You can extend section / key with a profile
392# For example, you want to deploy a demo version of your application without
393# HD content. You could first change the title to add "(demo)" in the name
394# and extend the excluded directories to remove the HD content.
395#
396#[app@demo]
397#title = My Application (demo)
398#
399#[app:source.exclude_patterns@demo]
400#images/hd/*
401#
402# Then, invoke the command line with the "demo" profile:
403#
404#buildozer --profile demo android debug