← ClaudeAtlas

android-file-storage-sharinglisted

File storage and sharing patterns for Android - scoped storage rules, app-specific storage with getExternalFilesDir and cacheDir, MediaStore API for images/video/audio/downloads, Storage Access Framework with ActivityResultContracts.OpenDocument and CreateDocument, FileProvider setup with provider_paths.xml for sharing content:// URIs via Intent.ACTION_SEND, DownloadManager for large background downloads, and safe handling of content:// vs file:// URIs. Use this skill whenever saving, reading, or sharing files, integrating with system file pickers, downloading large files, or configuring FileProvider for URI sharing. Trigger on phrases like "save file", "read file", "share file", "FileProvider", "MediaStore", "scoped storage", "download", "SAF", "content URI", "pick file", or "open document".
lenorebreakneck630/claude-zero-to-hero-android-KMP · ★ 1 · Data & Documents · score 64
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).