I’m developing a mobile application using Kivy Garden MapView, and I’m encountering difficulties accessing external storage for read and write operations specifically on Android devices. The app is designed to cache map tiles in a custom directory located in external storage.
I’ve implemented runtime permission handling to request both WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions using the Kivy Permission module. However, despite requesting these permissions, the app does not prompt users for storage access on Android devices, and consequently, it fails to access the designated cache directory.
Here’s a simplified version of my code relevant to storage access:
`def handle_permission_error():
dialog = MDDialog(title=”Permission Error”, text=”Please grant all required permissions for the app to function properly.”)
dialog.size_hint = [.8, .8]
dialog.pos_hint = {‘center_x’: .5, ‘center_y’: .5}
dialog.open()
def check_and_request_permissions():
if platform == ‘android’:
def callback(permission, results):
if all(results):
logging.info(“Got all permissions”)
else:
logging.warning(“Did not get all permissions”)
handle_permission_error()
request_permissions([Permission.WRITE_EXTERNAL_STORAGE,
Permission.READ_EXTERNAL_STORAGE,
Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION], callback)
logging.info("Checking and requesting permissions...")
else:
logging.warning("Permissions can only be requested on Android platform")
def setup_cache_directory(map_view):
cache_directory = None
if platform == 'android':
check_and_request_permissions()
max_attempts = 3
attempt = 0
while attempt < max_attempts:
# Set up cache directory after permissions are checked
try:
# Construct the path to the parent directory of "Download"
parent_dir = os.path.dirname(os.getenv('EXTERNAL_STORAGE'))
# Specify the name of your desired folder
folder_name = "MyApp"
# Construct the cache directory path
cache_directory = os.path.join(parent_dir, folder_name, 'cache_tiles')
print("Download directory FIRST:", parent_dir)
print("Cache directory FIRST:", cache_directory)
except Exception as e1:
print("Error getting download directory using os module:", e1)
attempt += 1
if attempt < max_attempts:
logging.info("Retrying attempt %d...", attempt)
time.sleep(1)
if cache_directory is None:
logging.error("Failed to access the download directory after multiple attempts.")
handle_permission_error()
elif platform == 'win':
logging.info("We are on the Windows platform")
current_directory = os.getcwd()
cache_directory = os.path.join(current_directory, 'cache_tiles')
logging.info("Cache directory: %s", cache_directory)
try:
os.makedirs(cache_directory, exist_ok=True)
map_view.cache_dir = cache_directory
except Exception as e:
logging.error("Error creating cache directory: %s", e)
cache_directory = None
else:
logging.warning("Platform is not Android or Windows. Unable to determine download folder.")
if cache_directory is not None:
map_view.cache_dir = cache_directory`
On Windows, the code works perfectly fine, and the cache directory is set up without any issues. However, on Android, despite implementing permission handling and constructing the cache directory path correctly (or so I believe), the app fails to access external storage.
I’ve verified that the necessary permissions are declared in the AndroidManifest.xml file, and I’m not receiving any errors during the execution of the code.
What could be causing this issue, and how can I ensure that my Kivy app properly accesses external storage for read and write operations on Android devices?
Any insights or suggestions would be greatly appreciated. Thank you!
fidelio1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.