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.
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:
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:
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:
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:
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.
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:
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.
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 AndroidUri. - Validate the scheme and host if the input must be a real web URL.
- Use
Uri.Builderfor dynamic URIs instead of string concatenation. - Treat file paths, content URIs, and web links as different cases.
- For app-to-app file sharing, prefer
FileProviderover rawfile://URIs.
Related reading
- Turning an ExecutorService to daemon in Java
- Tutorials on Core netty and Protobuf
- Type List vs type ArrayList in Java
- Unable to access Spring Boot Actuator /actuator endpoint
- Turning Firebase Analytics on on Xcode
- Tutorial for SLComposeViewController sharing
- Unable to acquire JDBC Connection
- Unable to compile simple Java 10 / Java 11 project with Maven

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.