Convert String to Uri

前端 未结 6 1591
余生分开走
余生分开走 2020-11-30 19:58

How can I convert a String to a Uri in Java (Android)? i.e.:

String myUrl = \"http://stackoverflow.com\";

myUri = ???;

6条回答
  •  日久生厌
    2020-11-30 20:45

    If you are using Kotlin and Kotlin android extensions, then there is a beautiful way of doing this.

    val uri = myUriString.toUri()
    

    To add Kotlin extensions (KTX) to your project add the following to your app module's build.gradle

      repositories {
        google()
    }
    
    dependencies {
        implementation 'androidx.core:core-ktx:1.0.0-rc01'
    }
    

提交回复
热议问题