diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-03-19 18:48:32 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-03-19 18:48:32 +0100 |
commit | 486e43dabd1198e6f3c94a4040fc01f5b2fe824c (patch) | |
tree | d328bf845d35e875c3c10964ac3b44d047ac352c /g4f/gui/server/android_gallery.py | |
parent | Fix abort in webview (diff) | |
download | gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.tar gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.tar.gz gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.tar.bz2 gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.tar.lz gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.tar.xz gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.tar.zst gpt4free-486e43dabd1198e6f3c94a4040fc01f5b2fe824c.zip |
Diffstat (limited to 'g4f/gui/server/android_gallery.py')
-rw-r--r-- | g4f/gui/server/android_gallery.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/g4f/gui/server/android_gallery.py b/g4f/gui/server/android_gallery.py new file mode 100644 index 00000000..9101bc02 --- /dev/null +++ b/g4f/gui/server/android_gallery.py @@ -0,0 +1,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)
\ No newline at end of file |