Developing a data logger app with file storage
An Android phone can act as a field recorder, capturing sensor readings, location fixes, button presses and status changes without sending every event to a server. File-based capture keeps records available offline, handles patchy coverage and provides a straightforward export path.
A dependable logger separates capture, formatting and storage. The interface reports what is recorded, a repository owns file operations, and a background mechanism keeps sampling when the screen is off. This structure makes the project easier to test across Android devices with different memory and battery limits.
Choosing a record format
JSON Lines suits append-only event logs because each object occupies one line and remains readable if a later line is damaged. CSV suits spreadsheet exports, while JSON handles nested fields such as coordinates, units and error values. Android’s built-in JSONObject creates the first format without another dependency.
data class LogEntry(
val timestampUtc: Long,
val source: String,
val value: Double,
val unit: String
)
fun LogEntry.toJsonLine(): String = JSONObject()
.put("timestampUtc", timestampUtc)
.put("source", source)
.put("value", value)
.put("unit", unit)
.toString()
Store timestamps in UTC and keep the original unit. This matters across Australia: a traveller moving between Sydney, Melbourne and Adelaide can cross states with different daylight-saving rules. Convert only for display, and add a sequence number so interrupted sessions remain ordered.
Building the capture screen
The screen should show the source, latest value, sample interval, destination file and recording state. A Start/Stop control and status list work well with Jetpack Compose or XML views. Keep readings in a ViewModel and send storage calls through a coroutine, keeping disk I/O off the main thread.
Request permissions when the selected source needs them. Precise location requires ACCESS_FINE_LOCATION on current Android releases, and foreground capture may also require a service. A commuter might log a journey using Opal in Sydney, Myki in Melbourne or a go card in Brisbane, while an Australian bushwalker may record temperature, altitude and track points; explain the permission before the system prompt.
Writing records without losing data
Use internal app storage for working logs, normally under filesDir/logs. Create a file per recording session and append UTF-8 text. Perform writes on Dispatchers.IO, serialise access with a Mutex and flush after important events.
private val writeMutex = Mutex()
suspend fun appendEntry(entry: LogEntry, file: File) {
writeMutex.withLock {
withContext(Dispatchers.IO) {
file.parentFile?.mkdirs()
FileOutputStream(file, true).bufferedWriter().use { writer ->
writer.appendLine(entry.toJsonLine())
}
}
}
}
This sample opens and closes the file for each entry, which suits low-frequency events but wastes I/O for high-rate sensors. For continuous capture, maintain one buffered writer, close it when stopping and recover the last complete line after a crash. Internal files are private to the app and removed on uninstall.
Export through Android’s Storage Access Framework: launch ACTION_CREATE_DOCUMENT, let the user choose a Downloads or cloud location, and copy the session there. This avoids broad storage permissions on current Android versions and keeps the user in control of the shared file.
When history needs indexed queries rather than line-by-line reading, a relational store is better. This Android SQLite example demonstrates inserting, filtering and retrieving records, while a file remains useful for export.
Comparing local storage choices
The right local store depends on how records are written, searched and retained.
| Storage option | Best fit | Strengths | Trade-off |
|---|---|---|---|
| JSON Lines file | Continuous sensor or event capture | Simple, appendable, portable | Queries require custom parsing |
| CSV file | Spreadsheet and engineering exports | Familiar to many users | Nested data and mixed types are awkward |
| SQLite or Room | Searchable history and filters | Indexes, relationships, typed access | More schema and migration work |
| SharedPreferences | Flags and small settings | Minimal code and overhead | Unsuitable for growing record sets |
A hybrid design works well: write the live session to a JSON Lines file, then import it into SQLite for historical filters. SharedPreferences should hold settings such as the selected sensor or interval, never the primary archive. Make size limits and cleanup policies visible before storage becomes a problem.
Keeping background capture reliable
A visible activity cannot guarantee uninterrupted logging. Use a foreground service for an ongoing session, start it promptly and show a persistent notification identifying the source and providing a Stop action. On Android 14 and later, declare the correct foreground-service type and meet its permission requirements; location capture commonly needs the location type and foreground location permission.
A repository should expose one append operation so the UI, service and retry path cannot write concurrently. Persist the session state and last record number, then detect a truncated final line on the next launch. WorkManager suits delayed uploads or summaries, not continuous capture. For customers on capped mobile plans, uploads should wait for Wi-Fi or an unmetered network.
Meeting Australian privacy expectations
Precise location, device identifiers and linked activity can be personal information. Where the Privacy Act 1988 and Australian Privacy Principles apply, make collection specific, retain only what the purpose requires, protect the data and allow access or correction. Explain local processing in the app and privacy policy instead of relying on a technical term such as “telemetry”.
If records are sent by SMS or email, the Spam Act 2003 can require consent, sender identification and a working unsubscribe mechanism. Paid storage or export features should state prices and renewal terms clearly under the Australian Consumer Law. Google Play can handle Australian billing, but platform distribution does not remove responsibility for accurate performance claims.
Internal storage is sandboxed, but not automatically encrypted. Records containing health, work or location details should use a key protected by Android Keystore, be excluded from inappropriate cloud backups and never reach Android’s system log. Keep secrets out of filenames, and make deletion remove session files and imported database rows.
Preparing a release-ready logger
Test several Android versions, low-memory devices and phones with little free space. Exercise screen rotation, process death, denied permissions, timezone changes, daylight-saving boundaries and cancellation during a write. Confirm that each exported line parses, the visible count matches the file and deleting a session removes temporary and archived copies.
Before publication, complete the Google Play Data Safety form, explain permissions in context and provide export or deletion. Test the installed build rather than only the IDE: release builds restrict debugging and file-access behaviour can differ. A paid tier should present Australian-dollar pricing that includes GST to Australian buyers.
For a field-ready release, the core path is predictable: the user starts a source, the app records an ordered local session, the service communicates its state and export creates a portable copy without exposing private app files. That sequence turns sensor callbacks into a logger developers can maintain and users can trust.