[Solved] Convert String to Uri

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

String myUrl = "";

myUri = ???;

Enquirer: Techboy


Solution #1:

You can use the parse static method from Uri


Uri myUri = Uri.parse("")
Respondent: ccheneson

Solution #2:

I am just using the package.
Here you can do the following:


String myUrl = "";
URI myURI = new URI(myUrl);
Respondent: Jürgen K.

Solution #3:

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 {

dependencies {
    implementation 'androidx.core:core-ktx:1.0.0-rc01'

Solution #4:

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

Uri myUri = Uri.parse("");

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){

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

Respondent: JSON C11

Solution #5:

Java’s parser in is going to fail if the URI isn’t fully encoded to its standards. For example, try to parse:|dog. An exception will be thrown for the vertical bar.

urllib makes it easy to convert a string to a It will pre-process and escape the URL.

Respondent: EricE

Solution #6:

What are you going to do with the URI?

If you’re just going to use it with an HttpGet for example, you can just use the string directly when creating the HttpGet instance.

HttpGet get = new HttpGet("");
Respondent: Stuart Grimshaw

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy