Closed tmolitor-stud-tu closed 6 months ago
done
I've added a test with a mocked android import (test_android_folder_from_p4a
) and I also added an adopted storage path to test_android_folder_from_sys_path
and adapted the implementation to do a regex fallback for adopted storage, too.
When using platformdirs in an android app located on adopted storage,
_android_folder()
inandroid.py
returnedNone
which lead platformdirs to believe that it doesn't run on android and use theUnix
flavor instead.In android.py#L126
autoclass("android.content.Context")
returns a class instead of an instance, which throws an exception on the next line trying to access an instance method namedgetFilesDir()
.That exception is catched and a fallback implementation invoked, that uses a regular expression to search in
sys.path
for an import path rooted in/data
. Apps installed on adopted storage don't have such paths but begin with/mnt/expand/
instead. The regular expression therefore fails and_android_folder()
returnsNone
.This pull request fixes the issue at least for cases where python4android is available, by first trying to import the
android
module and using it'smActivity
export to get hold of the main activity. That activity is then used to get a proper application context upon whichgetFilesDir()
can be invoked.If that fails with an exception the original codepath is taken, trying the
autoclass
approach next and if that fails resorting to the regular expression.