summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/android_gallery.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-03-19 18:59:42 +0100
committerGitHub <noreply@github.com>2024-03-19 18:59:42 +0100
commit79bc56f36e02a3ccb82c92af58363836ab2affb2 (patch)
tree60b32a4cae7f2921ddbb4e77560ac761164989ca /g4f/gui/server/android_gallery.py
parent~ | include /local/models dir on pip (diff)
parentFix Bing key error (diff)
downloadgpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.tar
gpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.tar.gz
gpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.tar.bz2
gpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.tar.lz
gpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.tar.xz
gpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.tar.zst
gpt4free-79bc56f36e02a3ccb82c92af58363836ab2affb2.zip
Diffstat (limited to 'g4f/gui/server/android_gallery.py')
-rw-r--r--g4f/gui/server/android_gallery.py67
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