Improve Article Show
Save Article Improve Article Save Article Android gives various options for storing apps data which uses a file system similar to the disk-based system on computer platforms
Developers are advised to use the options available to store data depending upon the space required, reliable data access, and privacy of data. The data files saved over external storage devices are publicly accessible on shared external storage using USB mass storage transfer. Data files stored over external storage using a FileOutputStream object and can be read using a FileInputStream object. External Storage AvailabilityIn order to avoid crashing the app first, we need to check storage SD Card is available for reading and write operations. The method getExternalStorageState() is used to determine the state of mounted storage media such as SD Card is missing, read-only or readable, and writable. Below is the code snippet which we will use to check the availability of external storage. Java
Methods to Store data in External Storage
ExampleIn this example, we would store text data into the external storage and fetch to see that data. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. Step by Step ImplementationStep 1: Create a New Project To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Step 2: Access Permission to External Storage To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest.xml file. Add these permissions just after the package name. XML
Step 3: Before creating the layout and corresponding java files let’s add a few string attributes which we are using in our layout files Go to app > res > values > string.xml and insert the following code snippet XML
Now go to app > res > values > colors.xml and change the color attributes as following in order to make App Bar more attractive. XML
Again go to app > res > drawable and create a new Drawable Resource File and name it as button_layout. In this, we are modifying our button style for a better UX/UI. XML
Step 4: Working with the activity_main.xml file Go to res > layout > activity_main.xml and write down the following code. In this layout file, we are creating a multiline EditText View for getting the data from users and Buttons to save that data over internal and external storage media. XML
Step 5: Working with the MainActivity.java file In MainActivity we define the functions that handled the onClick behavior of the buttons. And fetch the data from the EditText and save it in external storage publicly and privately. We also display a Toast message of the path where the data is stored. Java
Step 6: Create a new Empty Activity We will create a new activity and name it ViewInformationActivity. We use this activity to display the saved data from the external storage. So, first, we create a layout for this activity similar to the MainActivity layout. activity_view_information.xml layout code snippet: XML
Now, we will create a corresponding java code for ViewInformationActivity. In this, we define methods showPublicData() and showPrivateData() over buttons that will fetch the data from the files saved to external storage and add the data to buffer, and then populate the data to TextView in order to show them. ViewInformationAcitity.java code snippet: Java
Output: Run on Emulatorhttps://media.geeksforgeeks.org/wp-content/uploads/20201101122556/External-Storage-Tutorial-in-Android.mp4 How do I select external storage on Android? Open the Settings app.. Tap Apps.. Tap the app that you want to move.. Tap Storage.. Tap Change. Please note: if you do not see the change option, it may not be possible to move this app to an SD card.. Tap SD card.. Tap Move.. Where are the files stored in external storage in Android?Android External Storage Example Code
getExternalFilesDir(): It returns the path to files folder inside Android/data/data/application_package/ on the SD card. It is used to store any required files for your app (like images downloaded from web or cache files).
How do I view files on my SD card Android?Most Android phones come preinstalled with at least one file manager app. A file manager app lets you view and work with the files sitting on both internal and SD card storage on your device. This app may be called File Manager, Files, or something similar and should be available in the app drawer of your device.
How do I access other files on Android?Find & open files. Open your phone's Files app . Learn where to find your apps.. Your downloaded files will show. To find other files, tap Menu . To sort by name, date, type, or size, tap More. Sort by. If you don't see "Sort by," tap Modified or Sort .. To open a file, tap it.. |