9

How can I convert a String to a Uri in Kotlin instead of java (Android)?

val Uri_string = "/sdcard/example.jpg"

Uri_string = ???;

Felix
  • 93
  • 1
  • 1
  • 6

2 Answers2

15
val uri = Uri.parse(Uri_string)

or

val fileName = "example.jpg"
val path = "${Environment.getExternalStorageDirectory()}/$fileName"
val file = File(path); 
val uri = Uri.fromFile(file);
Artem Botnev
  • 2,267
  • 1
  • 14
  • 19
3

If you are using Android KTX you can also parse this way:

val uri_string = "/sdcard/example.jpg"
val uri = uri_string.toUri()