from kivy.app import App
from kivy.uix.scatter import Scatterfrom kivy.uix.label import Labelfrom kivy.uix.floatlayout import FloatLayoutfrom jnius import castfrom jnius import autoclassfrom kivy.uix.popup import Popup
class TutorialApp(App): def build(self): f = FloatLayout() ActivityResultListener = autoclass('org.renpy.android.PythonActivity$ActivityResultListener')
class ResultListener(ActivityResultListener): def onActivityResult(self, requestCode, resultCode, data): popup = Popup(title='Test popup', content=Label(text='Hello world'), auto_dismiss=True) popup.open()
PythonActivity = autoclass('org.renpy.android.PythonActivity') Intent = autoclass('android.content.Intent')
intent = Intent() intent.setAction(Intent.ACTION_PICK) intent.setType("image/*") current_activity = cast('android.app.Activity', PythonActivity.mActivity) current_activity.startActivity(intent) PythonActivity.registerActivityResultListener(ResultListener()) return f
if __name__ == "__main__": TutorialApp().run()I/python ( 7886): Traceback (most recent call last):I/python ( 7886): File "/home/ogh/tmp/apptest/.buildozer/android/app/main.py", line 37, in <module>I/python ( 7886): File "/home/ogh/tmp/apptest/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/app.py", line 766, in runI/python ( 7886): File "/home/ogh/tmp/apptest/.buildozer/android/app/main.py", line 18, in buildI/python ( 7886): File "jnius_export_class.pxi", line 36, in jnius.jnius.MetaJavaClass.__new__ (jnius/jnius.c:11778)I/python ( 7886): File "jnius_export_class.pxi", line 49, in jnius.jnius.MetaJavaClass.resolve_class (jnius/jnius.c:12049)I/python ( 7886): jnius.jnius.JavaException: __javaclass__ definition missingI/python ( 7886): Python for android ended.--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
But the API documentation for the Android Activity clearly features a registerActivityResultListener method and a ActivityResultListener class.
Sorry for forgetting to mention it before.
Here is a link
http://python-for-android.readthedocs.org/en/latest/javaapi/#activity
__version__='0.1'
from kivy.app import App
from kivy.uix.scatter import Scatterfrom kivy.uix.label import Labelfrom kivy.uix.floatlayout import FloatLayoutfrom jnius import castfrom jnius import autoclassfrom kivy.uix.popup import Popupfrom android import activity
class TutorialApp(App): def build(self): f = FloatLayout()
PythonActivity = autoclass('org.renpy.android.PythonActivity') Intent = autoclass('android.content.Intent')
intent = Intent() intent.setAction(Intent.ACTION_PICK) intent.setType("image/*") current_activity = cast('android.app.Activity', PythonActivity.mActivity) current_activity.startActivity(intent) activity.bind(on_activity_result=self.on_activity_result) return f def on_activity_result(self, requestCode, resultCode, data): print "### ACTIVITY CALLBACK ###" popup = Popup(title='Test popup', content=Label(text='Hello world'), auto_dismiss=True) popup.open()
if __name__ == "__main__": TutorialApp().run()I/ActivityManager( 588): Displayed android/com.android.internal.app.ResolverActivity: +289msI/PackageManager( 588): Action: "android.intent.action.PICK"I/PackageManager( 588): Category: "android.intent.category.DEFAULT"I/PackageManager( 588): Type: "image"I/PackageManager( 588): mPriority=0, mHasPartialTypes=trueI/PackageManager( 588): Adding preferred activity ComponentInfo{com.google.android.gallery3d/com.android.gallery3d.app.GalleryActivity} for user 0 :from kivy.uix.floatlayout import FloatLayoutfrom jnius import castfrom jnius import autoclassfrom android import activity
class TutorialApp(App): def build(self): f = FloatLayout() activity.bind(on_new_intent=self.on_new_intent,on_activity_result=self.on_activity_result)
PythonActivity = autoclass('org.renpy.android.PythonActivity') Intent = autoclass('android.content.Intent')
intent = Intent() intent.setAction(Intent.ACTION_PICK) intent.setType("image/*") current_activity = cast('android.app.Activity', PythonActivity.mActivity) current_activity.startActivity(intent) return f def on_activity_result(self, requestCode, resultCode, data): print "### ACTIVITY CALLBACK ###" def on_new_intent(self, intent): print "#### INTENT", intent
if __name__ == "__main__": TutorialApp().run()current_activity = cast('android.app.Activity', PythonActivity.mActivity)current_activity.startActivity(intent)PythonActivity.mActivity.startActivityForResult(intent, 0x123)
from kivy.uix.floatlayout import FloatLayoutfrom jnius import castfrom jnius import autoclassfrom android import activity
class TutorialApp(App): def build(self): f = FloatLayout() activity.bind(on_new_intent=self.on_new_intent,on_activity_result=self.on_activity_result)
PythonActivity = autoclass('org.renpy.android.PythonActivity') Intent = autoclass('android.content.Intent')
intent = Intent() intent.setAction(Intent.ACTION_PICK) intent.setType("image/*") PythonActivity.mActivity.startActivityForResult(intent, 0x123) return f def on_activity_result(self, requestCode, resultCode, data): print "### ACTIVITY CALLBACK ###" def on_new_intent(self, intent): print "#### INTENT", intent
from kivy.logger import Logger
from kivy.clock import Clock
from jnius import autoclass
from jnius import cast
# python-for-android provides this
from android import activity
PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
# Value of MediaStore.Images.Media.DATA
MediaStore_Images_Media_DATA = "_data"
# Custom request codes
RESULT_LOAD_IMAGE = 1
Activity = autoclass('android.app.Activity')
# Activity is only used to get these codes. Could just hardcode them.
# /** Standard activity result: operation canceled. */
# public static final int RESULT_CANCELED = 0;
# /** Standard activity result: operation succeeded. */
# public static final int RESULT_OK = -1;
# /** Start of user-defined activity results. */
# Not sure what this means
# public static final int RESULT_FIRST_USER = 1;
def user_select_image(callback):
"""Open Gallery Activity and call callback with absolute image filepath of image user selected.
None if user canceled.
"""
# PythonActivity.mActivity is the instance of the current Activity
# BUT, startActivity is a method from the Activity class, not from our
# PythonActivity.
# We need to cast our class into an activity and use it
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: callback(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 = [MediaStore_Images_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: callback(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)