Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get media files saved locally
/**
* From Google's CameraXbasic sample app's MainActivity.kt and GalleryFragment.kt
* See: [https://github.com/android/camera-samples/tree/master/CameraXBasic#cameraxbasic]
*/
val EXTENSION_WHITELIST = arrayOf("JPG")
/** 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
}
getOutputDirectory(someContext).listFiles { file ->
EXTENSION_WHITELIST.contains(file.extension.toUpperCase(Locale.ROOT))
}?.sortedDescending()?.toMutableList() ?: mutableListOf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment