How to read and write to external storage in latest android API>29 from Kivy python

Latest development in Android needs significant modifications in the way we seek permissions to read and write files in the external storage of android. Old method of seeking android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE will not work any more. Lets see the reason and the possible work around to resolve this in this post. The Python code with Kivy framework is considered for the explanation.

Why we can not read from android external storage with READ_EXTERNAL_STORAGE permission

For android versions 10 and below (API level 29 and below) can read the external storage by setting the requestLegacyExternalStorage to TRUE. However, this will not work when you target the android 11 (API level 30 and above) due to the strict permissions in reading and writing the files in external storage. The read and write permissions in android have been modified with a enforced scoped storage. This result in errors while reading and writing files in android external storage from kivy based app as given in the following posts

Do you need help in python app development
Do you need help in python app development

What is scoped storage in android

Scoped storage has been introduced with user privacy in mind. This means that the app developed by you will not have access to all the user data in the external storage. This was true in the earlier versions of Android and lower API levels. However, the sensitive user data that may not be relevant to your app is not required but may be present in the external storage. Hence you do not have access to those files. Good news is your app have access to the common directories in the external storage. You app has full read and write permission in the directory which is specific to your app (/app/data/<YourPackageName>). For this directory, you do not need to request read and write permissions. The folder are identified as private directories and shared directories. Your app directory is your private directory and no other apps have permission to access this. Similarly, you can not access other apps private directories. All other folder are in shared directories. You can not access these all even though they are not part of other apps private directories. In simple words, this means
  1. Your app has full access to its app specific directory
  2. Limited access in external storage (Still need to ask permission for this)
you can read media files (images, audio files) from the shared directories by asking permissions. Reading and writing of random files needs the usage of file chooser from Android. If you use kivy, this may be little difficult (still doable).
Further access can be requested with MANAGE_EXTERNAL_STORAGE . However, this permission is not given to all apps. This is given to the apps which falls into one of the categories below. If your app asks for MANAGE_EXTERNAL_STORAGE , it is possible that it gets rejected at play store, if it does not fall into one of these categories.
  1. File managers
  2. Backup and restore apps
  3. Anti-virus apps
  4. Document management apps
  5. On-device file search
  6. Disk and file encryption
  7. Device-to-device data migration

How to read and write files with kivy in latest android

Reading

To read the image files use the following code to ask for permissions to read images
from android.permissions import request_permissions, Permission
request_permissions([Permission.READ_MEDIA_IMAGES, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])

Permission.READ_MEDIA_IMAGES will request to read images from external storage. This is mandatory if you like to read images. Permission.READ_MEDIA_AUDIO and Permission.READ_MEDIA_VIDEO are also availe to read audio files and video files. With this you can read the video files. If you do not request these permissions, you will be ablke to see the directorories but no images will be shown. If you use these lines, the user will be shown the dialog asking for permissions as shown in the image below.

Dialog asking for read permissions in android app
Dialog asking for read permissions in android app

Writing

The shared folders like Documents, Pictures and Downloads are available for writing. You can use thesde folder to write the output. Following code wilol write the pdf image to documents folder after creating a folder inside documents folder. This code is a part of our image to pdf converter app

os.mkdir(primary_external_storage_path() + '/Documents/Image to PDF by Techris')
with open(primary_external_storage_path() + "/Documents/Image to PDF by Techris/output-" + time.strftime("%Y%m%d-%H%M%S") + ".pdf", "wb") as f:
f.write(img2pdf.convert(imgs))
print('Processed images')
This is how you can read and write in the external storage in latest android from Python and kivy.

Example Python kivy app that uses reads and writes in android external storage

We have developed a kivy based Android app that uses these techniques. The app is available here . You can get to read the entire source code. Visit this page to get the app and the source code.
Scroll to Top