Skip to content

Commit c63588d

Browse files
authored
Update storage.py fixes runtime errors
1) fixes isExternalStorageRemovable is expecting a file object, error was: ``` 07-28 16:36:46.845 2152 2784 I python : File "/home/andre/workspace/EtherollApp/.buildozer/android/platform/build/build/python-installs/etheroll/android/storage.py", line 61, in primary_external_storage_path 07-28 16:36:46.847 2152 2784 I python : File "jnius/jnius_export_class.pxi", line 1034, in jnius.jnius.JavaMultipleMethod.__call__ 07-28 16:36:46.848 2152 2784 I python : jnius.jnius.JavaException: No methods matching your arguments, available: ['()Z', '(Ljava/io/File;)Z'] ``` 2) fixes `os.path.exists()` doesn't accept `None`, error was: 07-28 22:31:25.877 31203 31313 I python : File "/home/andre/workspace/EtherollApp/.buildozer/android/platform/build/build/python-installs/etheroll/android/storage.py", line 101, in secondary_external_storage_path 07-28 22:31:25.878 31203 31313 I python : File "/home/andre/workspace/EtherollApp/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/genericpath.py", line 19, in exists 07-28 22:31:25.879 31203 31313 I python : TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType 3) fixes on services `PythonActivity.mActivity` is None, refs kivy/kivy#6388
1 parent 7d5fdee commit c63588d

File tree

1 file changed

+19
-7
lines changed
  • pythonforandroid/recipes/android/src/android

1 file changed

+19
-7
lines changed

pythonforandroid/recipes/android/src/android/storage.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
21
from jnius import autoclass, cast
32
import os
43

54

65
Environment = autoclass('android.os.Environment')
6+
File = autoclass('java.io.File')
77

88

99
def _android_has_is_removable_func():
@@ -24,6 +24,19 @@ def _get_sdcard_path():
2424
)
2525

2626

27+
def _get_activity():
28+
"""
29+
Retrieves the activity from `PythonActivity` fallback to `PythonService`.
30+
"""
31+
PythonActivity = autoclass('org.kivy.android.PythonActivity')
32+
activity = PythonActivity.mActivity
33+
if activity is None:
34+
# assume we're running from the background service
35+
PythonService = autoclass('org.kivy.android.PythonService')
36+
activity = PythonService.mService
37+
return activity
38+
39+
2740
def app_storage_path():
2841
""" Locate the built-in device storage used for this app only.
2942
@@ -32,9 +45,8 @@ def app_storage_path():
3245
3346
Returns directory path to storage.
3447
"""
35-
PythonActivity = autoclass('org.kivy.android.PythonActivity')
36-
currentActivity = cast('android.app.Activity',
37-
PythonActivity.mActivity)
48+
activity = _get_activity()
49+
currentActivity = cast('android.app.Activity', activity)
3850
context = cast('android.content.ContextWrapper',
3951
currentActivity.getApplicationContext())
4052
file_p = cast('java.io.File', context.getFilesDir())
@@ -58,7 +70,7 @@ def primary_external_storage_path():
5870
# Apparently this can both return primary (built-in) or
5971
# secondary (removable) external storage depending on the device,
6072
# therefore check that we got what we wanted:
61-
if not Environment.isExternalStorageRemovable(sdpath):
73+
if not Environment.isExternalStorageRemovable(File(sdpath)):
6274
return sdpath
6375
if "EXTERNAL_STORAGE" in os.environ:
6476
return os.environ["EXTERNAL_STORAGE"]
@@ -88,7 +100,7 @@ def secondary_external_storage_path():
88100
# Apparently this can both return primary (built-in) or
89101
# secondary (removable) external storage depending on the device,
90102
# therefore check that we got what we wanted:
91-
if Environment.isExternalStorageRemovable(sdpath):
103+
if Environment.isExternalStorageRemovable(File(sdpath)):
92104
if os.path.exists(sdpath):
93105
return sdpath
94106

@@ -98,6 +110,6 @@ def secondary_external_storage_path():
98110
p = os.environ["SECONDARY_STORAGE"]
99111
elif "EXTERNAL_SDCARD_STORAGE" in os.environ:
100112
p = os.environ["EXTERNAL_SDCARD_STORAGE"]
101-
if os.path.exists(p):
113+
if p is not None and os.path.exists(p):
102114
return p
103115
return None

0 commit comments

Comments
 (0)