Convert String to Uri

前端 未结 6 1595
余生分开走
余生分开走 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:53

    You can parse a String to a Uri by using Uri.parse() as shown below:

    Uri myUri = Uri.parse("http://stackoverflow.com");
    

    The following is an example of how you can use your newly created Uri in an implicit intent. To be viewed in a browser on the users phone.

    // 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);
    }
    

    NB: There is a difference between Androids URI and Uri.

提交回复
热议问题