Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if a Uri is empty?

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.

like image 698
Alex Voicu Avatar asked Feb 25 '15 23:02

Alex Voicu


3 Answers

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

        if (!Uri.EMPTY.equals(followUri)) {
            //handle followUri
        }
like image 186
Stanislav Bodnar Avatar answered Nov 20 '22 20:11

Stanislav Bodnar


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
}
like image 44
Michael Powell Avatar answered Nov 20 '22 20:11

Michael Powell


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.

like image 1
ci_ Avatar answered Nov 20 '22 20:11

ci_