Trouble accessing external storage for read and write operations in Kivy Garden MapView on Android

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!

New contributor

fidelio1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật