File size: 2,470 Bytes
79859e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
from kivy.logger import Logger
from kivy.clock import Clock
from jnius import autoclass
from jnius import cast
from android import activity
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
MEDIA_DATA = "_data"
RESULT_LOAD_IMAGE = 1
Activity = autoclass('android.app.Activity')
def user_select_image(on_selection):
"""Open Gallery Activity and call callback with absolute image filepath of image user selected.
None if user canceled.
"""
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
# Forum discussion: https://groups.google.com/forum/#!msg/kivy-users/bjsG2j9bptI/-Oe_aGo0newJ
def on_activity_result(request_code, result_code, intent):
if request_code != RESULT_LOAD_IMAGE:
Logger.warning('user_select_image: ignoring activity result that was not RESULT_LOAD_IMAGE')
return
if result_code == Activity.RESULT_CANCELED:
Clock.schedule_once(lambda dt: on_selection(None), 0)
return
if result_code != Activity.RESULT_OK:
# This may just go into the void...
raise NotImplementedError('Unknown result_code "{}"'.format(result_code))
selectedImage = intent.getData(); # Uri
filePathColumn = [MEDIA_DATA]; # String[]
# Cursor
cursor = currentActivity.getContentResolver().query(selectedImage,
filePathColumn, None, None, None);
cursor.moveToFirst();
# int
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
# String
picturePath = cursor.getString(columnIndex);
cursor.close();
Logger.info('android_ui: user_select_image() selected %s', picturePath)
# This is possibly in a different thread?
Clock.schedule_once(lambda dt: on_selection(picturePath), 0)
# See: http://pyjnius.readthedocs.org/en/latest/android.html
activity.bind(on_activity_result=on_activity_result)
intent = Intent()
# http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/
# http://stackoverflow.com/questions/18416122/open-gallery-app-in-android
intent.setAction(Intent.ACTION_PICK)
# TODO internal vs external?
intent.setData(Uri.parse('content://media/internal/images/media'))
# TODO setType(Image)?
currentActivity.startActivityForResult(intent, RESULT_LOAD_IMAGE) |