How to save a file in the internal storage of the Android device


Android / Saturday, August 31st, 2019

Simple App to save a file in the internal storage of the Android device

In this blog post, we are going to learn about how to save a file in the internal storage of the Android device. So for saving a file, you have two options, first one is you can save a file into the internal storage of the device and the second option you can save a file into the external storage of the device. The internal storage is the inbuilt memory of the device and the external storage may be a removable SD card.

So here we are going to learn about how to save a text file into the internal storage of the device. For saving a text file into the internal storage and external storage are different. We are going to learn about how to save a sample text file into the internal storage.

Following is the GUI of the application.

internal app GUI
From top to bottom, here we have a TextView, showing “Save To Internal Storage”. Next, we have an EdiTtext view, where we have to type the title of the message, then another EditText view to write our message to store the file in the internal storage of the android device. After that there are two Buttons, one is to save the message and another is for showing the saved message. Then there is a TextView to display the saved message.
Now we go to the activity_main.xml file to understand individual views:

ConstraintLayout:

We have used ContraintLayout as our main layout. We have set the layout background and padding as shown.

TextView (txtTitle):

In the txtTitle TextView, we constraint it to the parent layout. And set the text, textColor, textSize, textStyle and layout_marginTop attributes.

EditText (edtMessageTitle):

We have constraint edtMessageTitle EditText to the bottom of txtTitle TextView. Here we have used android:hint attribute to guide user, what he/ she has to input.

EditText (edtMessage):

edtMessage EditText is constraint at the bottom of the edtMessageTitle EditText.

Button (btnSave):

btnSave Button is constraint at the bottom of the edtMessage EditText. And all the other attributes are set as needed.

Button (btnShow):

btnShow Button is constraint at the bottom of the btnSave Button. And all the other attributes are set as needed.

TextView (txtShow):

txtShow view is constraint bottom of the btnShow Button.

Note:

There is nothing to do on the AndroidManifest.xml file. We no need to add any permission for writing information into the internal storage of the android device.

Now we move to MainActivity.java file to understand the actual coding part.
At first we have to create member variables for our views.

Next we have to link all the views of our activity_main.xml layout in the onCreate method, like the following code.

Next we have to create onClickListener method for our btnSave button. In the following method you will see that I have created two String variables, one messageTitle and another one is message. I have stored the text in the messageTitle from the edtMessageTitle view using getText method after converting it to String using toString method. Similarly, I have stored the text of edtMessage in the message String.

Now we have to create an object of FileOutputStream, to create the object I used openFileOutput method. For this method we have to pass two arguments, first one is the filename and second one is the opening mode.
Using the fileOutputStream object we will save the file in the internal storage of the android device using the write method which takes message.getBytes method as its argument. After writing/ storing the file we have to close fileOutputStream object. We have also shown a Toast message after storing the file.

To retrieve the stored file from internal storage we have to create an onClickListener method for btnShow button. Here in this method we have to create an object of FileInputStream using openFileInput method which takes file name as argument. Also we have created three more objects of InputSreamReader, BufferedReader and StringBuffer. Using bufferedReader object we will from the file and append the content to the stringBuffer object. To do that we have used while loop, until bufferedReader.readLine returns null. After that we display the file in txtShow view using setText method.

Hope you like that simple app. If you like, please share and comment.

Here is the project structure with complete code:

project structure

activity_main.xml

MainActivity.java

 

Leave a Reply

Your email address will not be published. Required fields are marked *