Building an Android File Explorer for Internal Storage
Android applications often need to let people browse documents they have created or chosen. An internal storage file explorer can show folders, photos, PDFs and downloads in a tree, while keeping each user's workspace inside the application. Building one also teaches URI permissions, scoped-storage rules, RecyclerView navigation and the differences between app-private files and shared device media.
The key design decision comes before the UI. Android's “internal storage” can mean the app's private filesDir, where no broad storage permission is required, or a folder on shared storage selected through the Storage Access Framework. Modern Android versions enforce that boundary, so the explorer should use the model that matches its purpose rather than relying on obsolete direct-file paths.
Understanding Android's storage boundaries
Private application storage sits inside the app sandbox, commonly under filesDir, cacheDir and noBackupFilesDir. The operating system prevents other apps and ordinary file managers from browsing it. Your app can read it directly with java.io.File, including File(filesDir, "notes/project.txt"), and recursively enumerate it with walkTopDown(). No READ_EXTERNAL_STORAGE permission is needed for these app-owned files.
Shared storage is different. Images, downloads and folders created by other applications are normally reached through a content provider, not by treating /storage/emulated/0 as unrestricted disk space. The Storage Access Framework grants access to a tree the user explicitly selects. The wider Android Open Tutorials collection includes related Android storage examples that can help bridge this concept with database and preferences work.
This distinction affects security and UX. An app that opens only its own documents can provide instant navigation but cannot present the phone's complete file system. An app that browses user-selected shared folders needs to request access, retain the resulting URI permission and communicate clearly which folder it can see.
Choosing a storage access strategy
Use DocumentFile for a Storage Access Framework tree because it presents files and directories through the provider's content URIs. Add the AndroidX DocumentFile dependency, keep the selected root URI in saved state and launch OpenDocumentTree from a button labelled with the current folder. Persistable access survives process death and reboots, so the user should not have to choose the same folder every time the app opens.
private val treeLauncher = registerForActivityResult(
ActivityResultContracts.OpenDocumentTree()
) { uri ->
if (uri != null) {
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(uri, flags)
browse(uri)
}
}
fun chooseFolder() {
treeLauncher.launch(null)
}
Handle SecurityException around permission persistence because some providers grant temporary access only. Keep the original URI as the root and use each directory's URI as navigation moves deeper. If the app exclusively manages app-private files, bypass the picker and build the adapter from filesDir; that simpler design has no storage permission or provider dependency.
Building the directory browser
Represent each row with a DocumentFile, its display name, type, size and modified time. A ListAdapter with DiffUtil keeps scrolling smooth when the visible folder changes. Sort directories before files, compare names without case sensitivity and label unknown MIME types as generic files. Avoid showing a zero size for directories, because providers often report directory size as zero.
private fun browse(uri: Uri) {
currentFolder = DocumentFile.fromTreeUri(this, uri)
render(currentFolder)
}
private fun render(folder: DocumentFile?) {
val rows = folder?.listFiles().orEmpty().sortedWith(
compareByDescending<DocumentFile> { it.isDirectory }
.thenBy { it.name?.lowercase() ?: "" }
)
adapter.submitList(rows)
}
Build navigation around parent and child relationships. Tapping a directory changes currentFolder and reloads the list; the back action moves to parentFile, while returning to the root restores the saved root URI. Tapping a document should issue an ACTION_VIEW intent with its content URI and a temporary read grant. This allows installed apps, including a PDF reader, to open it without exposing a raw filesystem path.
Adding safe file operations
A practical explorer should create, rename and delete documents while respecting the selected provider. DocumentsContract can call these operations directly when the provider supports them. Resolve names before showing edit controls, keep destructive actions behind an explicit confirmation and refresh the current directory after a successful change. Disable an operation that the provider does not support rather than showing a button that will fail.
private fun createNote(name: String) {
val parent = currentFolder?.uri ?: return
val uri = DocumentsContract.createDocument(
contentResolver,
parent,
"text/plain",
name
)
if (uri != null) {
contentResolver.openOutputStream(uri)?.use { stream ->
stream.write("Created on device".toByteArray())
}
render(currentFolder)
}
}
Rename and deletion use DocumentsContract.renameDocument and DocumentsContract.deleteDocument. Catch provider exceptions and show a concise failure message while leaving the directory unchanged. If documents may contain tax receipts, client details or health information, the Australian Privacy Act and Australian Privacy Principles may apply to the app's handling. Keep access scoped to the selected tree, avoid copying sensitive files into shared or exported locations and do not log document names or paths.
Testing for Australian Android conditions
Australia's primary Android marketplace is Google Play, and common devices include Samsung Galaxy handsets and Google Pixels. The mix is fragmented across manufacturers, screen sizes and supported Android versions, so test on a recent physical phone, an older device near the minimum supported API and an emulator. Verify that the folder picker opens, granted access survives a restart, back navigation reaches the root and documents open in other apps. Include providers beyond the emulator's Downloads folder, such as Google Drive or a file manager exposed as a document provider.
Localisation matters as well. Australian interfaces commonly use words such as colour, organise and favourites, while dates are often expected as 24/03/2026 rather than a US-style month-first order. Show modified timestamps in the relevant Australian zone: Brisbane uses AEST year-round, while Sydney, Melbourne, Adelaide and Tasmania observe daylight-saving shifts. Handle these time-zone changes because a file's local timestamp can appear to jump when it moves between regions.
Regional and mobile work makes offline access especially valuable. A builder travelling through regional Queensland may need quotations, certificates and site photos available without a connection, so the selected folder should remain usable with no network. Test long Australian place names, filenames containing spaces and apostrophes, and Unicode characters. A reliable file explorer for internal storage therefore combines scoped permissions, resilient navigation, clear error handling and storage behaviour that reflects how Android devices are actually used across Australia.