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 when prompted for the Activity Type. We will use the same layout as the ...

Get Android Application Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.