DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Android File Storage Guide — Internal, External & SAF Explained

What You'll Learn

How to use Android's file storage options: internal storage, scoped external storage, and Storage Access Framework.

Storage Types

Type API Permission
Internal context.filesDir None
External (app) getExternalFilesDir() None
User files SAF OpenDocument None
Gallery MediaStore None (Android 10+)

Internal Storage

// Write
context.openFileOutput("data.txt", Context.MODE_PRIVATE).use {
    it.write(content.toByteArray())
}
// Read
context.openFileInput("data.txt").bufferedReader().use { it.readText() }
Enter fullscreen mode Exit fullscreen mode

SAF — File Picker

val openLauncher = rememberLauncherForActivityResult(
    ActivityResultContracts.OpenDocument()
) { uri -> uri?.let { readTextFromUri(context, it) } }

Button(onClick = { openLauncher.launch(arrayOf("text/*")) }) {
    Text("Open File")
}
Enter fullscreen mode Exit fullscreen mode

MediaStore — Save to Gallery

val values = ContentValues().apply {
    put(MediaStore.Images.Media.DISPLAY_NAME, "photo.png")
    put(MediaStore.Images.Media.MIME_TYPE, "image/png")
    put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
}
val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
Enter fullscreen mode Exit fullscreen mode

Summary

  • Internal storage for app-private data
  • External app-specific dirs need no permissions
  • SAF for user-driven file operations
  • MediaStore for shared media

8 production-ready Android app templates on Gumroad.
Browse templatesGumroad

Top comments (0)