In this post, I’ll show you a deeper look into the Android file storage and how to perform file-related tasks.
All Android devices has 2 file storage areas:
Comparison:
Internal Storage | External Storage |
Always available | Â Only available if the removable storage was mounted into the device |
By default, the internal storage is only accessible by its own application | Â Word-readable |
If user uninstall the application, every data in internal storage will be deleted | If user uninstall the application, all the data which was not stored in getExternalFileDir() folder will remain |
Firstly, you have to call Context.openFileOutput() method to open a file in internal storage and return a FileOutputStream (create new file if it doesn’t exist):
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
This method takes 2 parameters:
Then, you can write the data in bytes format to created file and remember to close the stream after wrote:
public static void writeToInternalStorage(Context context, String fileName, byte[] data) throws IOException { FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); fos.write(data); fos.close(); }
In order to read data from a file, you must create a FileInputStream object by calling openFileInput() method:
FileInputStream fis = context.openFileInput(fileName);
This method takes file name as the only parameter.
Then, you can read data in bytes format from the input stream and remember to close it after read:
public static byte[] readFromInternalStorage(Context context, String fileName) throws IOException { FileInputStream fis = context.openFileInput(fileName); byte[] result = getBytesArrayFromInputStream(fis); fis.close(); return result; } /*** * Return a bytes array from the InputStream */ private static byte[] getBytesArrayFromInputStream(InputStream input) throws IOException { byte[] buffer = new byte[8192]; int bytesRead; ByteArrayOutputStream output = new ByteArrayOutputStream(); while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } return output.toByteArray(); }
Because the external storage is not always available, to make sure the application works properly, we have to check if the external storage is available or not:
public static boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } public static boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; }
In external storage, we have 2 types of directory:
You usually use public directory to store the data that can be used by other application (captured photos, downloaded files,…) and private directory for the data that is only meaningful to your app.
To write data to external storage:
public static void writeToExternalPublicFile(String dirType, String fileName, byte[] data) throws IOException { File file = new File(Environment.getExternalStoragePublicDirectory(dirType), fileName); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); } public static void writeToExternalPrivateFile(Context context, String dirType, String fileName, byte[] data) throws IOException { File file = new File(context.getExternalFilesDir(dirType), fileName); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); }
To read data to external storage:
public static byte[] readFromExternalPublicFile(String dirType, String fileName) throws IOException { File file = new File(Environment.getExternalStoragePublicDirectory(dirType), fileName); FileInputStream fis = new FileInputStream(file); byte[] result = getBytesArrayFromInputStream(fis); fis.close(); return result; } public static byte[] readFromExternalPrivateFile(Context context, String dirType, String fileName) throws IOException { File file = new File(context.getExternalFilesDir(dirType), fileName); FileInputStream fis = new FileInputStream(file); byte[] result = getBytesArrayFromInputStream(fis); fis.close(); return result; }
To apply the above codes, we will make a small demo application to show you how to work with File Storage in Android.
The application has 4 button to invoke different functions about read and write data in internal and external storage and a text view to display result:
After run the application, you can look up the file in device’s storage:
Internal:
External:
The whole source code can be found at my Github:
https://github.com/trinhlbk1991/DemoFileStorage
Comments powered by Disqus.