Consider using Kotlin's use function to automatically close the FileOutputStream. And Ensure that you have the necessary permissions to write to external storage, especially for Android 10 (API level 29) and above, where scoped storage is enforced. Plus Ensure FileOutputStream is closed in a finally block to prevent resource leaks. Source:-chatgpt