Kivy using python-for-android. Unable to retrieve data with startActivityForResult

593 views
Skip to first unread message

Davide Rosa

unread,
May 13, 2013, 8:18:43 AM5/13/13
to kivy-...@googlegroups.com
Hi all,

I have a problem with startActivityForResult. Basically I do not understand one thing. Python for android implements the PythonActivity class.
The kivy system implements the App class. But, is the Kivy App class a subclass of PythonActivity under android?

I need to take a picture using the android Camera intent. Here is a code piece:

PythonActivity = autoclass('org.renpy.android.PythonActivity')
MediaStore = autoclass('android.provider.MediaStore')
Intent = autoclass('android.content.Intent')
intent = Intent()
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE)
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
CAMERA_REQUEST = 1888   #1337
currentActivity.startActivityForResult(intent,CAMERA_REQUEST)

The intent starts correctly but, how can I get the result?

Supposing that the kivy's App class is a subclass of PythonActivity I overloaded the method onActivityResult. 
In this situation, the onActivityResult method is never called.

What is wrong? Have you a working code example?

Thank you in advance.

Davide Rosa

unread,
May 15, 2013, 5:44:01 AM5/15/13
to kivy-...@googlegroups.com
No reply... I'm loosing the hope.

However, I'm trying a different way to solve the problem. I think that onActivityResult is not called for the main App class because in PythonActivity,java it is not implemented and so it does not call the superclass method. In this situation I need to overload the java class Activity in this way:

class Activity(JavaClass):
__javaclass__ = 'android/app/Activity'
__metaclass__ = MetaJavaClass
startActivityForResult = JavaMultipleMethod(['(Landroid/content/Intent;I)V','(Landroid/content/Intent;ILandroid/os/Bundle;)V'])

def __init__(self, *args, **kwargs):
MediaStore = autoclass('android.provider.MediaStore')
Intent = autoclass('android.content.Intent')
intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
super(Activity,self).__init__(*args, **kwargs)
CAMERA_REQUEST = 1888

self.startActivityForResult(intent,CAMERA_REQUEST)
def onActivityResult(self, requestCode, resultCode, data):
print("ON ACTIVITY RESULT!!!")

I think that this is the right way. But right now there is a problem. jnius returns an error: "No methods matching your arguments"

I'm investigating on the causes. I will inform anyone interested as soon.

Cheers.  

Mathieu Virbel

unread,
May 19, 2013, 5:33:32 AM5/19/13
to kivy-...@googlegroups.com
Hi Davide,

I've _no idea_ how to overload the Activity in Python. And i don't think
you could create one from Python anyway:

http://stackoverflow.com/questions/6313610/dynamically-create-an-activity

So if you need to reference it from the Manifest, you also need a .class
corresponding to it.

Activity is like process, if you start a new Activity, it will be in a
seperated process, and this one will don't know anything about python etc.

I guess the right way would be to extend Python-for-android to provide
hooks on the onActivityResult. But we have one "big" limitation: The
PythonActivity / SDLSurfaceView cannot call python code directly. The
code is designed to let python drive the execution, not java.

One way to go would be to:
1. implement a onActivityResult() in PythonActivity (and PythonService
too), and store all the results in a list, as a ActivityResult class (to
be created too) with the requestCode, resultCode and data.

2. add a method pullActivityResult() that pop the result list and return
one entry. If no entry exist, just return null.

3. implement a python class to match that kind of usage, in the android
module:

ar = ActivityResult(callback=my_callback)
# and regulary call:
ar.poll()

4. in kivy, we could use it like that:

# do your intent stuff to start a new intent
# then:
ar = ActivityResult(callback=my_callback)
Clock.schedule_interval(ar.poll, 1)

# and as soon as we get the result, we could stop the scheduling as
well.


That's just a proposal, maybe you have a better design in the head, in
that case, don't hesitate to share!

Feel free to implement it and/or open an issue about it.

Thanks,

Mathieu

Le 15/05/2013 11:44, Davide Rosa a �crit :
> No reply... I'm loosing the hope.
>
> However, I'm trying a different way to solve the problem. I think that
> onActivityResult is not called for the main App class because in
> PythonActivity,java it is not implemented and so it does not call the
> superclass method. In this situation I need to overload the java class
> Activity in this way:
>
> class Activity(JavaClass):
> __javaclass__ = 'android/app/Activity'
> __metaclass__ = MetaJavaClass
> startActivityForResult =
> JavaMultipleMethod(['(Landroid/content/Intent;I)V','(Landroid/content/Intent;ILandroid/os/Bundle;)V'])
>
> def __init__(self, *args, **kwargs):
> MediaStore = autoclass('android.provider.MediaStore')
> Intent = autoclass('android.content.Intent')
> intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
> super(Activity,self).__init__(*args, **kwargs)
> CAMERA_REQUEST = 1888
>
> self.startActivityForResult(intent,CAMERA_REQUEST)
> def onActivityResult(self, requestCode, resultCode, data):
> print("ON ACTIVITY RESULT!!!")
>
> I think that this is the right way. But right now there is a problem.
> jnius returns an error: "No methods matching your arguments"
>
> I'm investigating on the causes. I will inform anyone interested as soon.
>
> Cheers.
>
> Il giorno luned� 13 maggio 2013 14:18:43 UTC+2, Davide Rosa ha scritto:
>
> Hi all,
>
> I have a problem with startActivityForResult. Basically I do not
> understand one thing. Python for android implements the
> PythonActivity class.
> The kivy system implements the App class. But, is the Kivy App class
> a subclass of PythonActivity under android?
>
> I need to take a picture using the android Camera intent. Here is a
> code piece:
>
> PythonActivity = autoclass('org.renpy.android.PythonActivity')
> MediaStore = autoclass('android.provider.MediaStore')
> Intent = autoclass('android.content.Intent')
> intent = Intent()
> intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE)
> currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
> CAMERA_REQUEST = 1888 #1337
> currentActivity.startActivityForResult(intent,CAMERA_REQUEST)
>
> The intent starts correctly but, how can I get the result?
>
> Supposing that the kivy's App class is a subclass of PythonActivity
> I overloaded the method onActivityResult.
> In this situation, the onActivityResult method is never called.
>
> What is wrong? Have you a working code example?
>
> Thank you in advance.
>
> --
> 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.
>
>

Reply all
Reply to author
Forward
0 new messages