File Storage - Internal Memory

By Kunal Kapoor
17-05-2018
1304

Android devices have 2 types of storages, Internal and external storage. Internal storage is easily accessible and Android provides free permission to read / write into internal memory.

Benefits of Internal Storage

  • It's always available.
  • Files saved here are accessible by only your app.
  • When the user uninstalls your app, the system removes all your app's files from internal storage


However there are some limitations of internal storage, that it can have less memory size. So large files and data cant be stored on it in some devices. Also if your application doesn't have much confidential files or files that need to be directly shared with other apps on the device, then you must use the external storage memory.


The following example will show you how to save a simple string in an File in Internal storage memory


Oveview:


Lets design a simple layout with 4 elemtns, one EditText to take the string to save, Textview to display the String data, and two Buttons to Save and Show actions.

activity_main.xml:

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="263dp"
android:layout_height="87dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.674" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Show"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.88" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="SAVE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.328" />

<EditText
android:id="@+id/editText"
android:layout_width="289dp"
android:layout_height="59dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />

android.support.constraint.ConstraintLayout
>


Lets start programming the java code.

First lets delcare object variables for all UI elements.

private EditText editText;
private TextView textView;

private Button SaveButton,ShowButton;

private String FileName="MyFile";


Instantiate the variables

editText=(EditText)findViewById(R.id.editText);
textView=(TextView)findViewById(R.id.textView);
SaveButton=(Button)findViewById(R.id.button2);
ShowButton=(Button)findViewById(R.id.button);

Set the OnClick listeners for the buttons and create methods for each

ShowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveTheData();
}
});

ShowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowTheData();
}
});


Program the SaveTheData() method in following way to save the string in edittext field:

String savetext=editText.getText().toString();
FileOutputStream outputStream;

try {
outputStream = openFileOutput(FileName, Context.MODE_PRIVATE);
outputStream.write(savetext.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

Toast.makeText(this,"File Saved",Toast.LENGTH_SHORT).show();


To display the saved string in textview field, program the ShowTheData() method :

Context context=getApplication();
String data = "";

try {
InputStream inputStream = context.openFileInput(FileName);

if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();

while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}

inputStream.close();
data = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
} catch (IOException e) {
}

textView.setText(data);
Toast.makeText(this,"Data Display",Toast.LENGTH_SHORT).show();


Complete code : MainActivity.java:

package com.ourcoaching.savingfiles_internalstorage;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

private EditText editText;
private TextView textView;

private Button SaveButton,ShowButton;

private String FileName="MyFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText=(EditText)findViewById(R.id.editText);
textView=(TextView)findViewById(R.id.textView);
SaveButton=(Button)findViewById(R.id.button2);
ShowButton=(Button)findViewById(R.id.button);

SaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveTheData();
}
});

ShowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowTheData();
}
});
}

private void ShowTheData(){

} Context context=getApplication();
String data = "";

try {
InputStream inputStream = context.openFileInput(FileName);

if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();

while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}

inputStream.close();
data = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
} catch (IOException e) {
}

textView.setText(data);
Toast.makeText(this,"Data Display",Toast.LENGTH_SHORT).show();

private void SaveTheData() {
String savetext=editText.getText().toString();
FileOutputStream outputStream;

try {
outputStream = openFileOutput(FileName, Context.MODE_PRIVATE);
outputStream.write(savetext.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

Toast.makeText(this,"File Saved",Toast.LENGTH_SHORT).show();
}
}


Download the code from GitHub



Related Tutorials