Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Uri in Kotlin [duplicate]

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

val Uri_string = "/sdcard/example.jpg"

Uri_string = ???;

like image 724
Felix Avatar asked Jun 04 '19 13:06

Felix


2 Answers

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);
like image 112
Artem Botnev Avatar answered Nov 07 '22 13:11

Artem Botnev


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

val uri_string = "/sdcard/example.jpg"
val uri = uri_string.toUri()
like image 2
Edgardo Barría Melián Avatar answered Nov 07 '22 13:11

Edgardo Barría Melián