Table of Contents
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.
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:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#009688" android:padding="10dp" tools:context=".MainActivity"> </androidx.constraintlayout.widget.ConstraintLayout>
We have used ContraintLayout as our main layout. We have set the layout background and padding as shown.
TextView (txtTitle):
<TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Save To Internal Storage" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
In the txtTitle TextView, we constraint it to the parent layout. And set the text, textColor, textSize, textStyle and layout_marginTop attributes.
EditText (edtMessageTitle):
<EditText android:id="@+id/edtMessageTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="#fff" android:hint="Enter Message Title" android:gravity="top" android:padding="10dp" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtTitle" />
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):
<EditText android:id="@+id/edtMessage" android:layout_width="match_parent" android:layout_height="180dp" android:layout_marginTop="20dp" android:background="#fff" android:gravity="top" android:hint="Enter Your Message" android:padding="10dp" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.157" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtMessageTitle" />
edtMessage EditText is constraint at the bottom of the edtMessageTitle EditText.
Button (btnSave):
<Button android:id="@+id/btnSave" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Save Message" android:textSize="18sp" android:background="#FFC107" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtMessage" />
btnSave Button is constraint at the bottom of the edtMessage EditText. And all the other attributes are set as needed.
Button (btnShow):
<Button android:id="@+id/btnShow" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Show Message" android:textSize="18sp" android:background="#FFC107" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnSave" />
btnShow Button is constraint at the bottom of the btnSave Button. And all the other attributes are set as needed.
TextView (txtShow):
<TextView android:id="@+id/txtShow" android:layout_width="match_parent" android:layout_height="180dp" android:background="#fff" android:gravity="top" android:padding="10dp" android:textSize="20sp" android:layout_marginTop="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.157" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnShow" />
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.
// ToDo: declare member variables EditText edtMessageTitle, edtMessage; Button btnSave, btnShow; TextView txtShow;
Next we have to link all the views of our activity_main.xml layout in the onCreate method, like the following code.
// ToDo: Linking the elements in the layout to Java code. edtMessageTitle = findViewById(R.id.edtMessageTitle); edtMessage = findViewById(R.id.edtMessage); btnSave = findViewById(R.id.btnSave); btnShow = findViewById(R.id.btnShow); txtShow = findViewById(R.id.txtShow);
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.
// ToDo: creating onClickListener for save button btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String messageTitle = edtMessageTitle.getText().toString(); String message = edtMessage.getText().toString(); try { FileOutputStream fileOutputStream = openFileOutput(messageTitle, MODE_PRIVATE); fileOutputStream.write(message.getBytes()); fileOutputStream.close(); Toast.makeText(getApplicationContext(), "Message Saved", Toast.LENGTH_LONG).show(); edtMessage.setText(""); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } });
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.
// ToDo: creating onClickListener for show button btnShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { String message; FileInputStream fileInputStream = openFileInput(edtMessageTitle.getText().toString()); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer stringBuffer = new StringBuffer(); while((message=bufferedReader.readLine())!=null){ stringBuffer.append(message + "\n"); } txtShow.setText(stringBuffer.toString()); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } });
Hope you like that simple app. If you like, please share and comment.
Here is the project structure with complete code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#009688" android:padding="10dp" tools:context=".MainActivity"> <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Save To Internal Storage" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/edtMessageTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="#fff" android:hint="Enter Message Title" android:gravity="top" android:padding="10dp" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtTitle" /> <EditText android:id="@+id/edtMessage" android:layout_width="match_parent" android:layout_height="180dp" android:layout_marginTop="20dp" android:background="#fff" android:gravity="top" android:hint="Enter Your Message" android:padding="10dp" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.157" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtMessageTitle" /> <Button android:id="@+id/btnSave" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Save Message" android:textSize="18sp" android:background="#FFC107" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtMessage" /> <Button android:id="@+id/btnShow" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Show Message" android:textSize="18sp" android:background="#FFC107" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnSave" /> <TextView android:id="@+id/txtShow" android:layout_width="match_parent" android:layout_height="180dp" android:background="#fff" android:gravity="top" android:padding="10dp" android:textSize="20sp" android:layout_marginTop="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.157" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnShow" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package in.livedu.rnyrj.savefiletointernalstorage; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { // ToDo: declare member variables EditText edtMessageTitle, edtMessage; Button btnSave, btnShow; TextView txtShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ToDo: Linking the elements in the layout to Java code. edtMessageTitle = findViewById(R.id.edtMessageTitle); edtMessage = findViewById(R.id.edtMessage); btnSave = findViewById(R.id.btnSave); btnShow = findViewById(R.id.btnShow); txtShow = findViewById(R.id.txtShow); // ToDo: creating onClickListener for save button btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String messageTitle = edtMessageTitle.getText().toString(); String message = edtMessage.getText().toString(); try { FileOutputStream fileOutputStream = openFileOutput(messageTitle, MODE_PRIVATE); fileOutputStream.write(message.getBytes()); fileOutputStream.close(); Toast.makeText(getApplicationContext(), "Message Saved", Toast.LENGTH_LONG).show(); edtMessage.setText(""); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); // ToDo: creating onClickListener for show button btnShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { String message; FileInputStream fileInputStream = openFileInput(edtMessageTitle.getText().toString()); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer stringBuffer = new StringBuffer(); while((message=bufferedReader.readLine())!=null){ stringBuffer.append(message + "\n"); } txtShow.setText(stringBuffer.toString()); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } }
;