Android Uri Tutorial and Examples.
A Uri
is a mutable URI reference. A URI reference includes a URI and a fragment, the component of the URI following a ‘#’.
Uri class performs little to no validation. It does this intentially for the sake of performance. Behavior is undefined for invalid input. Uri class is not strict with respect to invalid inputs. It prefers to return garbage rather than throw an exception unless otherwise specified.
Uri API Definition
It is an abstract class deriving directly from the java.lang.Object
. Moreover it implements the Parcelable
as well as Comparable<Uri>
interfaces:
public abstract class Uri extends Object implements Parcelable, Comparable<Uri>
Here’s it’s inheritance hierarchy:
java.lang.Object ↳ android.net.Uri
Important Uri Methods
(a). fromFile()
This method will create a Uri from a file. The URI has the form “file://”. Encodes path characters with the exception of ‘/’.
Example: “file:///tmp/android.txt”
Here’s it’s signature:
public static Uri fromFile (File file)
(b). fromParts
This method will create an opaque Uri
from the given components.
public static Uri fromParts (String scheme, String ssp, String fragment)
Quick Android Uri Examples
1. How to get the Extension from a Uri
Let’s say you provide us with a Context object as well as a Uri and we wish to get the extension for it and return as a string.
Here’s how we do it:
/** * To find out the extension of required object in given uri */ private static String getMimeType(@NonNull Context context, @NonNull Uri uri) { String extension; //Check uri format to avoid null if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { //If scheme is a content final MimeTypeMap mime = MimeTypeMap.getSingleton(); extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri)); } else { //If scheme is a File //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters. extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString()); } return extension; }
Oclemy
PickiT
This is an Android library that you can use to return real paths from Uri’s.
Follow the following steps to use it.
Step 1 – Installation
The library is hosted in jitpack so go to your project-level build.gradle and specify jitpack URL:
Then go to your app level build.gradle and add the dependency then sync:
Step 2 – Usage
Implement PickiTCallback as follows:
Then:
So for example assume that using your Filepicker you’ve selected the file and now you want to convert the Uri to a real path inside the onActivityResult:
NB/= If you are targeting SDK 29> add
android:requestLegacyExternalStorage="true"
in your manifest:The reason for this is, in Android 10 file path access was removed and it returned in Android 11.
If you are targiting SDK 29> and you do not add this, you will get
EACCES (Permission denied)
FULL EXAMPLE
Here is a full example.
(a). MainActivity.java
Demo
Download
Here are the links:
Oclemy
Some Utility Uri Examples
Here are some important utility methods you may need when coding.
(a). Get Path from Uri
You need to return a real path as a string and you have a Uri.
(b). Get Data Column when given Uri
(c). Get Downloads Path
You have a Uri and you want to return the downloads path.
Here are more examples:
Download
Here are the links: