Read and write a text file to external storage
The process of reading and writing files to external storage is basically the same as using internal storage. The difference is in obtaining a reference to the storage location. Also, as mentioned in the Introduction, external storage may not be available, so it's best to check availability before attempting to access it.
This recipe will read and write a text file, as we did in the previous recipe. We'll also demonstrate how to check the external storage state before we access it.
Getting ready
Create a new project in Android Studio and call it ExternalStorageFile
. Use the default Phone & Tablet
options and select Empty Activity
on the Add an Activity
to Mobile
dialog. We will use the same layout as the previous recipe, so you can just copy and paste if you typed it in already. Otherwise, use the layout from step 1 in the previous recipe, Read and write a text file to internal storage.
How to do it...
As mentioned previously in the Getting ready...