android-file-storage-sharinglisted
Install: claude install-skill lenorebreakneck630/claude-zero-to-hero-android-KMP
# Android File Storage and Sharing
## Core Principles
- On Android 10+ (API 29+), apps cannot freely write to shared storage. Use MediaStore or the Storage Access Framework.
- Never expose `file://` URIs to other apps — they will throw `FileUriExposedException` on API 24+. Use `FileProvider` instead.
- App-specific storage (`filesDir`, `cacheDir`, `getExternalFilesDir`) requires no permissions at any API level.
- Prefer MediaStore for media that should appear in system Gallery/Files apps; prefer SAF for user-chosen document locations.
- Always close streams in `finally` blocks or use `use {}` — leaking file descriptors causes silent data corruption.
---
## Storage Location Decision Tree
```
Does the file need to be visible to other apps or survive uninstall?
├── No → App-specific storage (filesDir / cacheDir / getExternalFilesDir)
└── Yes → Is it a photo, video, audio, or generic download?
├── Yes → MediaStore
└── No → Storage Access Framework (user picks the location)
```
---
## App-Specific Storage
No permissions required. Files are deleted when the app is uninstalled.
### Internal storage (always private)
```kotlin
// Write
fun writeInternalFile(context: Context, filename: String, content: String) {
context.openFileOutput(filename, Context.MODE_PRIVATE).use { stream ->
stream.write(content.toByteArray())
}
}
// Read
fun readInternalFile(context: Context, filename: String): String {
return context.openFileInput(filename).