
from jnius import autoclass, cast
import os


Environment = autoclass('android.os.Environment')


def _android_has_is_removable_func():
    VERSION = autoclass('android.os.Build$VERSION')
    return (VERSION.SDK_INT >= 24)


def _get_sdcard_path():
    """ Internal function to return getExternalStorageDirectory()
        path. This is internal because it may either return the internal,
        or an external sd card, depending on the device.
        Use primary_external_storage_path()
        or secondary_external_storage_path() instead which try to
        distinguish this properly.
    """
    return (
        Environment.getExternalStorageDirectory().getAbsolutePath()
    )


def app_storage_path():
    """ Locate the built-in device storage used for this app only.

        This storage is APP-SPECIFIC, and not visible to other apps.
        It will be wiped when your app is uninstalled.

        Returns directory path to storage.
    """
    PythonActivity = autoclass('org.kivy.android.PythonActivity')
    currentActivity = cast('android.app.Activity',
                           PythonActivity.mActivity)
    context = cast('android.content.ContextWrapper',
                   currentActivity.getApplicationContext())
    file_p = cast('java.io.File', context.getFilesDir())
    return os.path.normpath(os.path.abspath(
        file_p.getAbsolutePath().replace("/", os.path.sep)))


def primary_external_storage_path():
    """ Locate the built-in device storage that user can see via file browser.
        Often found at: /sdcard/

        This is storage is SHARED, and visible to other apps and the user.
        It will remain untouched when your app is uninstalled.

        Returns directory path to storage.

        WARNING: You need storage permissions to access this storage.
    """
    if _android_has_is_removable_func():
        sdpath = _get_sdcard_path()
        # Apparently this can both return primary (built-in) or
        # secondary (removable) external storage depending on the device,
        # therefore check that we got what we wanted:
        if not Environment.isExternalStorageRemovable(sdpath):
            return sdpath
    if "EXTERNAL_STORAGE" in os.environ:
        return os.environ["EXTERNAL_STORAGE"]
    raise RuntimeError(
        "unexpectedly failed to determine " +
        "primary external storage path"
    )


def secondary_external_storage_path():
    """ Locate the external SD Card storage, which may not be present.
        Often found at: /sdcard/External_SD/

        This storage is SHARED, visible to other apps, and may not be
        be available if the user didn't put in an external SD card.
        It will remain untouched when your app is uninstalled.

        Returns None if not found, otherwise path to storage.

        WARNING: You need storage permissions to access this storage.
                 If it is not writable and presents as empty even with
                 permissions, then the external sd card may not be present.
    """
    if _android_has_is_removable_func:
        # See if getExternalStorageDirectory() returns secondary ext storage:
        sdpath = _get_sdcard_path()
        # Apparently this can both return primary (built-in) or
        # secondary (removable) external storage depending on the device,
        # therefore check that we got what we wanted:
        if Environment.isExternalStorageRemovable(sdpath):
            if os.path.exists(sdpath):
                return sdpath

    # See if we can take a guess based on environment variables:
    p = None
    if "SECONDARY_STORAGE" in os.environ:
        p = os.environ["SECONDARY_STORAGE"]
    elif "EXTERNAL_SDCARD_STORAGE" in os.environ:
        p = os.environ["EXTERNAL_SDCARD_STORAGE"]
    if os.path.exists(p):
        return p
    return None

@@ -1,9 +1,8 @@
-%0A
 from jni
@@ -89,16 +89,49 @@
 nment')%0A
+File = autoclass('java.io.File')%0A
 %0A%0Adef _a
@@ -710,24 +710,461 @@
 h()%0A    )%0A%0A%0A
+def _get_activity():%0A    %22%22%22%0A    Retrieves the activity from %60PythonActivity%60 fallback to %60PythonService%60.%0A    %22%22%22%0A    PythonActivity = autoclass('org.kivy.android.PythonActivity')%0A    activity = PythonActivity.mActivity%0A    if activity is None:%0A        # assume we're running from the background service%0A        PythonService = autoclass('org.kivy.android.PythonService')%0A        activity = PythonService.mService%0A    return activity%0A%0A%0A
 def app_stor
@@ -1415,39 +1415,33 @@
 ge.%0A    %22%22%22%0A    
-PythonA
+a
 ctivity = autocl
@@ -1438,51 +1438,22 @@
 y = 
-autoclass('org.kivy.android.PythonA
+_get_a
 ctivity
-'
+(
 )%0A  
@@ -1504,53 +1504,10 @@
 ty',
-%0A                           PythonActivity.mA
+ a
 ctiv
@@ -2515,39 +2515,45 @@
 torageRemovable(
+File(
 sdpath)
+)
 :%0A            re
@@ -3810,23 +3810,29 @@
 movable(
+File(
 sdpath)
+)
 :%0A      
@@ -4159,23 +4159,41 @@
 ORAGE%22%5D%0A
-
     if 
+p is not None and 
 os.path.

Update storage.py fixes runtime errors