1

I am using the following method which I borrowed from this official camera github app:

    /** Use external media if it is available, our app's file directory otherwise */
    fun getOutputDirectory(context: Context): File {
        val appContext = context.applicationContext
        val mediaDir = context.externalMediaDirs.firstOrNull()?.let {
            File(it, appContext.resources.getString(R.string.app_name)).apply { mkdirs() } }
        return if (mediaDir != null && mediaDir.exists())
            mediaDir else appContext.filesDir
    }

But now Android Studio tells me that externalsMediaDirs is deprecated. How I change it so that it works?

abdullah celik
  • 511
  • 1
  • 6
  • 17
  • You do not have to change anything as deprecated does not mean that it does not work. Did you try? Please tell which path(s) it gives. – blackapps Feb 24 '21 at 14:04
  • Please see this answer it will help you https://stackoverflow.com/a/57116787/14884388 – Hascher Feb 25 '21 at 17:06

1 Answers1

1

Usually deprecation comes with a recommendation for a newer API and/or a reason why it is blocked.

This is the deprecation message from Android Developers:

These directories still exist and are scanned, but developers are encouraged to migrate to inserting content into a MediaStore collection directly, as any app can contribute new media to MediaStore with no permissions required, starting in Build.VERSION_CODES.Q.

According to the message it seems like those directories are safe to use (for now, at least). However, newer API is available which does not require storage permission (That MediaStore).

Shlomi Katriel
  • 2,103
  • 1
  • 7
  • 20