How do I know if a Uri is empty?

14,990

Solution 1

Just check that Empty URI is not equal to followUri, this check includes check by null:

        if (!Uri.EMPTY.equals(followUri)) {
            //handle followUri
        }

Solution 2

If the Uri itself is null then calling followUri.equals(Object) will result in a NPE. A better solution:

if (followUri != null && !followUri.equals(Uri.EMPTY)) {
    //doTheThing()
} else {
    //followUri is null or empty
}

Solution 3

If your app crashes with an NPE at that stage followUri is most likely null.

Also Uri objects are immutable, see here http://developer.android.com/reference/android/net/Uri.html so you cannot "reset" it.

Share:
14,990
Alex Voicu
Author by

Alex Voicu

Updated on July 28, 2022

Comments

  • Alex Voicu
    Alex Voicu almost 2 years

    I have an application where I need to be able to let the user decide how to put a picture profile, (camera or gallery). I have only one variable of type "Uri" but I do not understand how to reset it and make it EMPTY, or check if it is EMPTY. Because when I choose a picture I have to have the ability to change the photo if you do not like the one just put.
    I have tested as

     if (followUri.equals (Uri.EMPTY)) {...}
    

    but the application crashes with a null point exception.