Tec*_*boy 160 java string android uri
如何在Java(Android)中将String转换为Uri?即:
String myUrl = "http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
myUri = ???;
cch*_*son 365
您可以使用parse静态方法Uri
Uri myUri = Uri.parse("http://stackoverflow.com")
Run Code Online (Sandbox Code Playgroud)
Jür*_* K. 28
我只是在用这个java.net 包.您可以在此处执行以下操作:
String myUrl = "http://stackoverflow.com";
URI myURI = new URI(myUrl);
Run Code Online (Sandbox Code Playgroud)
您可以使用Uri.parse()将字符串解析为 Uri,如下所示:
Uri myUri = Uri.parse("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
以下是如何在隐式意图中使用新创建的 Uri 的示例。在用户手机上的浏览器中查看。
// Creates a new Implicit Intent, passing in our Uri as the second paramater.
Intent webIntent = new Intent(Intent.ACTION_VIEW, myUri);
// Checks to see if there is an Activity capable of handling the intent
if (webIntent.resolveActivity(getPackageManager()) != null){
startActivity(webIntent);
}
Run Code Online (Sandbox Code Playgroud)
import java.net.URI;
Run Code Online (Sandbox Code Playgroud)
以下也适用于我:
URI uri = URI.create("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
或者
URI uri = new URI("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Kotlin和Kotlin android扩展,那么有一种不错的方法。
val uri = myUriString.toUri()
Run Code Online (Sandbox Code Playgroud)
要将Kotlin扩展(KTX)添加到您的项目中,请将以下内容添加到应用模块的build.gradle中
repositories {
google()
}
dependencies {
implementation 'androidx.core:core-ktx:1.0.0-rc01'
}
Run Code Online (Sandbox Code Playgroud)