Using Local File Systems in Android Native Development

Mobterest Studio
3 min readJul 19, 2023

--

Using Local File Systems in Android Native Development

In Android native development, the ability to read from and write to the local file system is crucial for many applications. Whether it’s storing user preferences, caching data, or handling file operations, understanding how to work with local file systems is an essential skill. In this article, we will explore the basics of using local file systems in Android native development and provide code examples to illustrate the concepts.

Photo by Daria Nepriakhina 🇺🇦 on Unsplash

Prerequisites

To follow along with this tutorial, you should have a basic understanding of Android native development using Java or Kotlin. You should also have Android Studio installed on your machine.

Create a New Project

To get started, open Android Studio and create a new Android project. Once the project is created, we can begin working with the local file system.

Request Permission

Before accessing the local file system, it is important to request the necessary permissions from the user. In this example, we will request the “WRITE_EXTERNAL_STORAGE” permission, which allows us to write to the external storage.

In the AndroidManifest.xml file, add the following permission declaration within the <manifest> tag:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

To request the permission at runtime, add the following code in your MainActivity Activity class

private static final int PERMISSION_REQUEST_CODE = 1;

private void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted
} else {
// Permission denied
}
}
}

// Call requestStoragePermission() method in your desired location

Write to the Local File System

To write data to the local file system, we can use the java.io package. In this example, we will create a file named "example.txt" in the external storage directory and write some text into it.

private void writeFileToStorage() {
String filename = "example.txt";
String content = "Hello, World!";

try {
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileWriter writer = new FileWriter(file);
writer.append(content);
writer.flush();
writer.close();
Toast.makeText(this, "File saved successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error occurred while saving the file.", Toast.LENGTH_SHORT).show();
}
}

// Call writeFileToStorage() method in your desired location

Read from the Local File System

To read data from the local file system, we can use the java.io package. In this example, we will read the contents of the "example.txt" file created in the previous step.

private void readFileFromStorage() {
String filename = "example.txt";

try {
File file = new File(Environment.getExternalStorageDirectory(), filename);
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder content = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}

reader.close();
Toast.makeText(this, "File content: " + content.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error occurred while reading the file.", Toast.LENGTH_SHORT).show();
}
}

// Call readFileFromStorage() method in your desired location

Conclusion

In this article, we have learned the basics of using local file systems in Android native development. We covered how to request permission to access the file system, how to write data to a file, and how to read data from a file. By understanding these concepts and utilizing the provided code examples, you can effectively work with the local file system in your Android applications. Remember to handle exceptions and ensure that you have the necessary permissions for file operations to avoid any unexpected behavior or errors.

👏🏽 Give this story a CLAP

👉🏽 Subscribe for upcoming articles

💰 Access Free Mobile Development tutorials

🔔 Follow for more

See you on next article 👋

--

--

No responses yet