Android
Uri
String Conversion
Android Development
Java

Turning a string into a Uri in Android

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

On Android, turning a string into a Uri is usually as simple as calling Uri.parse(...). The part that matters is not the conversion itself, but choosing the right URI form for the job: web links, app deep links, and file-sharing URIs have different rules and should not all be treated as interchangeable strings.

The Basic Conversion

For a normal URL or URI string, use android.net.Uri.parse:

java
1import android.net.Uri;
2
3Uri uri = Uri.parse("https://example.com/products?id=42");
4System.out.println(uri.getScheme());  // https
5System.out.println(uri.getHost());    // example.com
6System.out.println(uri.getQueryParameter("id"));  // 42

This is the standard Android API for converting a textual URI into a Uri object. It is commonly used with Intent, networking, and content APIs.

Using the Parsed Uri in an Intent

One of the most common reasons to parse a URI string is to open it with another app:

java
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://developer.android.com"));
startActivity(intent);

That works well for well-formed HTTP or HTTPS links. If the string may be user-provided, validate it before launching the intent.

Uri.parse Does Not Validate Business Meaning

Uri.parse creates a Uri object from the string, but it does not magically make the URI safe or appropriate for your use case. For example:

java
Uri uri = Uri.parse("not a real url");
System.out.println(uri);  // not a real url

That is still a Uri object. It just is not a useful web URI. If your code requires a secure network URL, validate the scheme and host explicitly:

java
1Uri uri = Uri.parse(input);
2
3if (!"https".equals(uri.getScheme()) || uri.getHost() == null) {
4    throw new IllegalArgumentException("Expected a valid HTTPS URL");
5}

This is especially important for deep links, redirects, or untrusted input.

Prefer Uri.Builder for Dynamic URIs

If you are assembling a URI from pieces, use Uri.Builder rather than manual string concatenation. It handles encoding more safely and keeps the structure explicit.

java
1Uri uri = new Uri.Builder()
2        .scheme("https")
3        .authority("example.com")
4        .appendPath("search")
5        .appendQueryParameter("q", "android uri parsing")
6        .appendQueryParameter("page", "1")
7        .build();
8
9System.out.println(uri.toString());

This is safer than building "https://example.com/search?q=" + query by hand, because reserved characters in query values are encoded correctly.

File Paths Are a Different Case

If your string is a local file path, do not assume Uri.parse(path) is the best answer. For plain path strings, Uri.fromFile(...) can construct a file URI:

java
1import android.net.Uri;
2import java.io.File;
3
4File file = new File("/sdcard/Download/report.pdf");
5Uri fileUri = Uri.fromFile(file);

However, modern Android file sharing between apps should generally use FileProvider, not raw file:// URIs, because direct file URIs can break across app boundaries and trigger FileUriExposedException.

Content URIs and App Data

Not all URIs are web links or file paths. Android also uses content:// URIs to represent data exposed through a ContentProvider.

java
Uri contactsUri = Uri.parse("content://com.android.contacts/contacts");

These URIs are common when working with media, contacts, documents, or app-specific shared data. The fact that the string parses successfully still does not guarantee that your app has permission to access the underlying content.

Common Pitfalls

The biggest mistake is assuming Uri.parse validates correctness. It parses structure into a Uri object, but it does not enforce that the input is a safe or reachable web address. If you care about scheme, host, or app-allowed destinations, validate those fields yourself.

Another common issue is building query strings manually. This often causes broken encoding when values contain spaces, slashes, or other reserved characters. Uri.Builder is a better default for dynamically assembled URIs.

Finally, be careful with file paths. A Uri created from a local file path is not the same thing as a content URI or a web URI, and Android's cross-app file-sharing rules make that distinction important.

Summary

  • Use Uri.parse(...) to convert a URI string into an Android Uri.
  • Validate the scheme and host if the input must be a real web URL.
  • Use Uri.Builder for dynamic URIs instead of string concatenation.
  • Treat file paths, content URIs, and web links as different cases.
  • For app-to-app file sharing, prefer FileProvider over raw file:// URIs.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.