Internal Storage Demo in Android
This blog post demonstrates how to create a simple Android application that saves and loads text from internal storage. The application features a user-friendly interface with buttons to save text, load text, and display the file location.
XML Layout Files
activity_internal_storage.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_bg"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/linear_border"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="4dp"
android:padding="12dp"
android:gravity="center"
android:text="Internal Storage Demo"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/rounded_bg"
android:elevation="4dp"
android:hint="Enter text to save"
android:padding="12dp"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_bg"
android:elevation="6dp"
android:padding="12dp"
android:text="Save to File"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_bg"
android:elevation="6dp"
android:padding="12dp"
android:text="Load from File"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<Button
android:id="@+id/btnLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_bg"
android:elevation="6dp"
android:padding="12dp"
android:text="Show File Location"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/textview_bg"
android:elevation="4dp"
android:padding="12dp"
android:text="Loaded content will appear here"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<TextView
android:id="@+id/tvLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/textview_bg"
android:elevation="4dp"
android:padding="12dp"
android:text="File location will appear here"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
linear_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#20000000"/>
<stroke android:width="3dp" android:color="#FFFFFF"/>
<corners android:radius="16dp"/>
</shape>
textview_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#66000000"/>
<stroke android:width="2dp" android:color="#FFFFFF"/>
<corners android:radius="12dp"/>
<padding android:left="12dp" android:top="8dp" android:right="12dp" android:bottom="8dp"/>
</shape>
Java Activity File
InternalStorageActivity.java
package com.tecvipul.databasedemo;
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.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class InternalStorageActivity extends AppCompatActivity {
private EditText editText;
private TextView textView, tvLocation;
private static final String FILE_NAME = "myfile.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_storage);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
tvLocation = findViewById(R.id.tvLocation);
Button btnSave = findViewById(R.id.btnSave);
Button btnLoad = findViewById(R.id.btnLoad);
Button btnLocation = findViewById(R.id.btnLocation);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveToFile(editText.getText().toString());
}
});
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFromFile();
}
});
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileLocation();
}
});
}
private void saveToFile(String text) {
try (FileOutputStream fos = openFileOutput(FILE_NAME, MODE_PRIVATE)) {
fos.write(text.getBytes());
Toast.makeText(this, "Saved successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error saving file", Toast.LENGTH_SHORT).show();
}
}
private void loadFromFile() {
try (FileInputStream fis = openFileInput(FILE_NAME)) {
int size;
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[1024];
while ((size = fis.read(buffer)) != -1) {
sb.append(new String(buffer, 0, size));
}
textView.setText(sb.toString());
Toast.makeText(this, "File loaded!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error loading file", Toast.LENGTH_SHORT).show();
}
}
private void showFileLocation() {
File file = new File(getFilesDir(), FILE_NAME);
String filePath = file.getAbsolutePath();
tvLocation.setText("File Location: " + filePath);
}
}
Conclusion
This simple Android application demonstrates how to use internal storage to save and retrieve data. The user interface is designed to be intuitive, allowing users to easily save text, load it back, and view the file's location on the device.
0 Comments
If you have any doubts, Please let me know