I am developing some lib in Kotlin, I am not finding this solution,
How can I resolve this issue, It's saying that create extension function File?.plus, In java it works fine but in Kotlin how to write this code?
I am developing some lib in Kotlin, I am not finding this solution,
How can I resolve this issue, It's saying that create extension function File?.plus, In java it works fine but in Kotlin how to write this code?
You don't need to invent more code, just change the plus (+) to a comma(,).
val sdcard = File(Environment.getExternalStorageDirectory(), "/PicTaker/Images")
You have three options:
1) Avoid that you concat a File
with a String
by calling the toString()
what results in concating two strings.
val sdCard = File(Environment.getExternalStorageDirectory().toString() + "/PicTaker/Images")
2) Write that extension-function and return a string
private operator fun File?.plus(s: String): String {
return this.toString() + s
}
3) Use string template
val sdCard = File("${Environment.getExternalStorageDirectory()}/PicTaker/Images")
Add toString()
:
val sdcard = File(Environment.getExternalStorageDirectory().toString() + "...")
In Kotlin, we can do operator overloading for + and Kotlin compiler thinks you are trying to use the overloaded plus operator for File?
type. And provides an option to create the extension function to File?