RedApparat / Fotoapparat

Making Camera for Android more friendly. 📸
Apache License 2.0
3.81k stars 405 forks source link

Scoped storage: Support to save shared media files. #393

Closed carlosmuvi closed 4 years ago

carlosmuvi commented 4 years ago

Problem

Due to Scoped Storage, we can’t write the image directly to shared storage locations via direct file path. This approach is deprecated in android Q and will be enforced to stop using in 2020.

From Google's Scoped Storage best practices:

Storing shared media files. For apps that handle files that users expect to be sharable with other apps (such as photos) and be retained after the app has been uninstalled, use the MediaStore API. There are specific collections for common media files: Audio, Video, and Images. For other file types, you can store them in the new Downloads collection. To access files from the Downloads collection, apps must use the system picker.

Storing app-internal files. If your app is designed to handle files not meant to be shared with other apps, store them in your package-specific directories. This helps keep files organized and limit file clutter as the OS will manage cleanup when the app is uninstalled. Calls to Context.getExternalFilesDir() will continue to work.

We can still use saveToFile, making sure the constructed file path is within the scoped storage of the app. As stated before, we just have to make sure we use Context.getExternalFilesDir() to save in scoped storage.

However, there's no way to save in the shared media collection locations, since that has to be done via the usage of MediaStore.

Possible solution

As an alternative to the existing PhotoResult#saveToFile, a new PhotoResult#saveToImagesMediaStore (open to other names / function signatures) could be exposed.

TODO