-2

I am developing some lib in Kotlin, I am not finding this solution,

enter image description here

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67

3 Answers3

6

You don't need to invent more code, just change the plus (+) to a comma(,).

val sdcard = File(Environment.getExternalStorageDirectory(), "/PicTaker/Images")
Les
  • 10,335
  • 4
  • 40
  • 60
5

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/Image‌​s")
guenhter
  • 11,255
  • 3
  • 35
  • 66
3

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?

Bob
  • 13,447
  • 7
  • 35
  • 45