Android Uri

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;
    } 
Share

2 Examples

  1. 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:

     

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

     

    Then go to your app level build.gradle and add the dependency then sync:

     

    implementation 'com.github.HBiSoft:PickiT:0.1.14'

     

    Step 2 – Usage

    Implement PickiTCallback as follows:

     

    public class MainActivity extends AppCompatActivity implements PickiTCallbacks {
    

    Then:

     

        //Declare PickiT
        PickiT pickiT;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Initialize PickiT
            //context, listener, activity
            pickiT = new PickiT(this, this, this);
    
        }
    }

     

    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:

     

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_VIDEO_REQUEST) {
            if (resultCode == RESULT_OK) {
                pickiT.getPath(data.getData(), Build.VERSION.SDK_INT);
    
            }
        }
    }

     

    NB/= If you are targeting SDK 29> add android:requestLegacyExternalStorage="true" in your manifest:

     

    <manifest ... >
      <application 
        android:requestLegacyExternalStorage="true" 
      </application>
    </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

     

    import android.Manifest;
    import android.annotation.SuppressLint;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.os.Build;
    import android.os.Environment;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.view.ContextThemeWrapper;
    import androidx.core.app.ActivityCompat;
    import androidx.core.content.ContextCompat;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.hbisoft.pickit.PickiT;
    import com.hbisoft.pickit.PickiTCallbacks;
    
    public class MainActivity extends AppCompatActivity implements PickiTCallbacks {
        //Permissions
        private static final int SELECT_VIDEO_REQUEST = 777;
        private static final int PERMISSION_REQ_ID_RECORD_AUDIO = 22;
        private static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = PERMISSION_REQ_ID_RECORD_AUDIO + 1;
    
        //Declare PickiT
        PickiT pickiT;
    
        //Views
        Button button_pick;
        TextView pickitTv, originalTv, originalTitle, pickitTitle;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            init();
            buttonClickEvent();
    
            //Initialize PickiT
            pickiT = new PickiT(this, this, this);
    
        }
    
        //Show Toast
        private void showLongToast(final String msg) {
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
        }
    
        private void init() {
            button_pick = findViewById(R.id.button_pick);
            pickitTv = findViewById(R.id.pickitTv);
            originalTv = findViewById(R.id.originalTv);
            originalTitle = findViewById(R.id.originalTitle);
            pickitTitle = findViewById(R.id.pickitTitle);
        }
    
        private void buttonClickEvent() {
            button_pick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openGallery();
    
                    //  Make TextView's invisible
                    originalTitle.setVisibility(View.INVISIBLE);
                    originalTv.setVisibility(View.INVISIBLE);
                    pickitTitle.setVisibility(View.INVISIBLE);
                    pickitTv.setVisibility(View.INVISIBLE);
                }
            });
        }
    
        private void openGallery() {
            //  first check if permissions was granted
            if (checkSelfPermission()) {
                Intent intent;
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                } else {
                    intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
                }
                //  In this example we will set the type to video
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.putExtra("return-data", true);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivityForResult(intent, SELECT_VIDEO_REQUEST);
            }
        }
    
        //  Check if permissions was granted
        private boolean checkSelfPermission() {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, MainActivity.PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE);
                return false;
            }
            return true;
        }
    
        //  Handle permissions
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //  Permissions was granted, open the gallery
                    openGallery();
                }
                //  Permissions was not granted
                else {
                    showLongToast("No permission for " + Manifest.permission.WRITE_EXTERNAL_STORAGE);
                }
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == SELECT_VIDEO_REQUEST) {
                if (resultCode == RESULT_OK) {
    
                    //  Get path from PickiT (The path will be returned in PickiTonCompleteListener)
                    //
                    //  If the selected file is from Dropbox/Google Drive or OnDrive:
                    //  Then it will be "copied" to your app directory (see path example below) and when done the path will be returned in PickiTonCompleteListener
                    //  /storage/emulated/0/Android/data/your.package.name/files/Temp/tempDriveFile.mp4
                    //
                    //  else the path will directly be returned in PickiTonCompleteListener
                    pickiT.getPath(data.getData(), Build.VERSION.SDK_INT);
    
                    originalTv.setText(String.valueOf(data.getData()));
    
                }
            }
        }
    
        //
        //  PickiT Listeners
        //
        //  The listeners can be used to display a Dialog when a file is selected from Dropbox/Google Drive or OnDrive.
        //  The listeners are callbacks from an AsyncTask that creates a new File of the original in /storage/emulated/0/Android/data/your.package.name/files/Temp/
        //
        //  PickiTonUriReturned()
        //  When selecting a file from Google Drive, for example, the Uri will be returned before the file is available(if it has not yet been cached/downloaded).
        //  Google Drive will first have to download the file before we have access to it.
        //  This can be used to let the user know that we(the application), are waiting for the file to be returned.
        //
        //  PickiTonStartListener()
        //  This will be call once the file creations starts and will only be called if the selected file is not local
        //
        //  PickiTonProgressUpdate(int progress)
        //  This will return the progress of the file creation (in percentage) and will only be called if the selected file is not local
        //
        //  PickiTonCompleteListener(String path, boolean wasDriveFile)
        //  If the selected file was from Dropbox/Google Drive or OnDrive, then this will be called after the file was created.
        //  If the selected file was a local file then this will be called directly, returning the path as a String
        //  Additionally, a boolean will be returned letting you know if the file selected was from Dropbox/Google Drive or OnDrive.
    
        ProgressBar mProgressBar;
        TextView percentText;
        private AlertDialog mdialog;
        ProgressDialog progressBar;
    
        @Override
        public void PickiTonUriReturned() {
            progressBar = new ProgressDialog(this);
            progressBar.setMessage("Waiting to receive file...");
            progressBar.setCancelable(false);
            progressBar.show();
        }
    
        @Override
        public void PickiTonStartListener() {
            if (progressBar.isShowing()){
                progressBar.cancel();
            }
            final AlertDialog.Builder mPro = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
            @SuppressLint("InflateParams") final View mPView = LayoutInflater.from(this).inflate(R.layout.dailog_layout, null);
            percentText = mPView.findViewById(R.id.percentText);
    
            percentText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    pickiT.cancelTask();
                    if (mdialog != null && mdialog.isShowing()) {
                        mdialog.cancel();
                    }
                }
            });
    
            mProgressBar = mPView.findViewById(R.id.mProgressBar);
            mProgressBar.setMax(100);
            mPro.setView(mPView);
            mdialog = mPro.create();
            mdialog.show();
    
        }
    
        @Override
        public void PickiTonProgressUpdate(int progress) {
            String progressPlusPercent = progress + "%";
            percentText.setText(progressPlusPercent);
            mProgressBar.setProgress(progress);
        }
    
        @Override
        public void PickiTonCompleteListener(String path, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String reason) {
    
            if (mdialog != null && mdialog.isShowing()) {
                mdialog.cancel();
            }
    
            //  Check if it was a Drive/local/unknown provider file and display a Toast
            if (wasDriveFile){
                showLongToast("Drive file was selected");
            }else if (wasUnknownProvider){
                showLongToast("File was selected from unknown provider");
            }else {
                showLongToast("Local file was selected");
            }
    
            //  Chick if it was successful
            if (wasSuccessful) {
                //  Set returned path to TextView
                pickitTv.setText(path);
    
                //  Make TextView's visible
                originalTitle.setVisibility(View.VISIBLE);
                originalTv.setVisibility(View.VISIBLE);
                pickitTitle.setVisibility(View.VISIBLE);
                pickitTv.setVisibility(View.VISIBLE);
            }else {
                showLongToast("Error, please see the log..");
                pickitTv.setVisibility(View.VISIBLE);
                pickitTv.setText(reason);
            }
        }
    
    
        //
        //  Lifecycle methods
        //
    
        //  Deleting the temporary file if it exists
        @Override
        public void onBackPressed() {
            pickiT.deleteTemporaryFile(this);
            super.onBackPressed();
        }
    
        //  Deleting the temporary file if it exists
        //  As we know, this might not even be called if the system kills the application before onDestroy is called
        //  So, it is best to call pickiT.deleteTemporaryFile(); as soon as you are done with the file
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (!isChangingConfigurations()) {
                pickiT.deleteTemporaryFile(this);
            }
        }
    
    }

    Demo

    Download

    Here are the links:

    1. Direct Download here.
    2. Read more here.
    3. Follow author here.
  2. 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.

    public static String getPath(final Context context, final Uri uri) {
                //check for KITKAT or above
                final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    
                // DocumentProvider
                if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
                    // ExternalStorageProvider
                    if (isExternalStorageDocument(uri)) {
                        final String docId = DocumentsContract.getDocumentId(uri);
                        final String[] split = docId.split(":");
                        final String type = split[0];
    
                        if ("primary".equalsIgnoreCase(type)) {
                            return Environment.getExternalStorageDirectory() + "/" + split[1];
                        }
                    }
                    // DownloadsProvider
                    else if (isDownloadsDocument(uri)) {
                        return getDownloadFilePath(context, uri);
                    }
                    // MediaProvider
                    else if (isMediaDocument(uri)) {
                        final String docId = DocumentsContract.getDocumentId(uri);
                        final String[] split = docId.split(":");
                        final String type = split[0];
    
                        Uri contentUri = null;
                        if ("image".equals(type)) {
                            contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                        } else if ("video".equals(type)) {
                            contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                        } else if ("audio".equals(type)) {
                            contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                        }
    
                        final String selection = "_id=?";
                        final String[] selectionArgs = new String[] {
                                split[1]
                        };
    
                        return getDataColumn(context, contentUri, selection, selectionArgs);
                    }
                }
    
                // MediaStore (and general)
                else if ("content".equalsIgnoreCase(uri.getScheme())) {
                    // Return the remote address
                    if (isGooglePhotosUri(uri))
                        return uri.getLastPathSegment();
    
                    return getDataColumn(context, uri, null, null);
                }
                // File
                else if ("file".equalsIgnoreCase(uri.getScheme())) {
                    return uri.getPath();
                }
    
                return null;
            }

     

    (b). Get Data Column when given Uri

     

    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {
    
        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

     

    (c). Get Downloads Path

    You have a Uri and you want to return the downloads path.

     

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static String getDownloadFilePath(Context context, Uri uri){
        Cursor cursor = null;
        final String[] projection = {
                MediaStore.MediaColumns.DISPLAY_NAME
        };
        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                String fileName=cursor.getString(0);
                String path =Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
                if (!TextUtils.isEmpty(path)){
                    return path;
                }
            }
        } finally {
            cursor.close();
        }
        String id = DocumentsContract.getDocumentId(uri);
        if (id.startsWith("raw:")) {
            return id.replaceFirst("raw:", "");
        }
        Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads"), java.lang.Long.valueOf(id));
    
        return getDataColumn(context, contentUri, null, null);
    }

     

    Here are more examples:

     

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }
    
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }
    
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
    
    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

     

    Download

    Here are the links:

    1. Direct Download this java file here.
    2. Follow Author here.



Share an Example

Share an Example

Browse
What is the capital of Egypt? ( Cairo )