I am not very well versed with Kotlin and I am preferring Java over Kotlin while learning Android. But I am stuck at a piece of code which is as follows:
private fun getOutputDirectory(): File {
val mediaDir = externalMediaDirs.firstOrNull()?.let {
File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
return if (mediaDir != null && mediaDir.exists())
mediaDir else filesDir
}
The following function is called in onCreate method as var outputDirectory: File = getOutputDirectory();
Can you please help me convert the code into Java and in understanding it? Thank you.
CodePudding user response:
here is the process on how to convert Kotlin code to Java code. https://www.geeksforgeeks.org/how-to-convert-kotlin-code-to-java-code-in-android-studio/
But I suggest you to learn Kotlin as it is faster and preffered for Android app development.
CodePudding user response:
private File getOutputDirectory() {
File mediaDir = null;
if (getExternalMediaDirs().size > 0) {
mediaDir = new File(getExternalMediaDirs()[0], getResources().getString(R.string.app_name));
mediaDir.mkdirs();
}
return if (mediaDir != null && mediaDir.exists())
mediaDir
else
filesDir
}