Internal Storage:
Internal storage in Android implies the physical storage area that is always available but contains data that is personal to your application.
It is suitable for storing files and data that should not be visible or downloadable by any other application or other people.
Like all other operating systems, Android too provides each of the application with private user storage area.e.
Key points about Android Internal Storage:
Private to the App: Data stored in the internal storage is private to the application. Other apps cannot access this data directly.
File Storage: It is commonly used for storing small files, such as text files, databases, or configuration files, required by the app.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InternalStorageExample"
android:padding="16dp"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your message" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:gravity="center">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Data"
android:textColor="@color/colorWhite"
android:textAllCaps="false"
android:background="@color/colorOrange"
android:padding="16dp"
android:layout_margin="16dp" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Data"
android:textColor="@color/colorWhite"
android:textAllCaps="false"
android:background="@color/colorOrange"
android:padding="16dp"
android:layout_margin="16dp" />
</LinearLayout>
<TextView
android:id="@+id/textDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="20dp"
android:padding="16dp"
android:gravity="center" />
</LinearLayout>
package com.example.widgets;
import android.content.Context;
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.FileOutputStream;
import java.io.InputStreamReader;
public class InternalStorageExample extends AppCompatActivity {
EditText editText;
Button save, display;
TextView textView;
String filename = "myfile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_storage_example);
editText = findViewById(R.id.input);
save = findViewById(R.id.b1);
display = findViewById(R.id.b2);
textView = findViewById(R.id.textDisplay);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = editText.getText().toString().trim();
if (!msg.equals("")) {
try {
FileOutputStream fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
fileOutputStream.write(msg.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Data saved successfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Data is empty", Toast.LENGTH_SHORT).show();
}
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileInputStream fileInputStream = openFileInput(filename);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String msg;
StringBuilder stringBuilder = new StringBuilder();
while ((msg = bufferedReader.readLine()) != null) {
stringBuilder.append(msg);
}
textView.setText(stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
package com.example.widgets
import android.content.Context
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 androidx.appcompat.app.AppCompatActivity
import java.io.BufferedReader
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStreamReader
class InternalStorageExample : AppCompatActivity() {
private lateinit var editText: EditText
private lateinit var save: Button
private lateinit var display: Button
private lateinit var textView: TextView
private val filename = "myfile"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_internal_storage_example)
editText = findViewById(R.id.input)
save = findViewById(R.id.b1)
display = findViewById(R.id.b2)
textView = findViewById(R.id.textDisplay)
save.setOnClickListener {
val msg = editText.text.toString().trim()
if (msg.isNotEmpty()) {
try {
val fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE)
fileOutputStream.write(msg.toByteArray())
fileOutputStream.close()
Toast.makeText(applicationContext, "Data saved successfully", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
e.printStackTrace()
}
} else {
Toast.makeText(applicationContext, "Data is empty", Toast.LENGTH_SHORT).show()
}
}
display.setOnClickListener {
try {
val fileInputStream = openFileInput(filename)
val inputStreamReader = InputStreamReader(fileInputStream)
val bufferedReader = BufferedReader(inputStreamReader)
var msg: String?
val stringBuilder = StringBuilder()
while (bufferedReader.readLine().also { msg = it } != null) {
stringBuilder.append(msg)
}
textView.text = stringBuilder.toString()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
Output