简体   繁体   中英

Check BitmapDescriptor for null

I want to check BitmapDescriptor for null.

The documentation for BitmapDescriptorFactory.fromAsset states that:

Returns the BitmapDescriptor that was loaded from the asset or null if failed to load.

    BitmapDescriptor bd = BitmapDescriptorFactory.fromAsset("markerimages/filename.png");
    if (bd == null) {
        // doSomething...
    }

Despite this, I am unable to catch BitMapDescriptor in a null state - even when I pass a filename that does not exist, the fromAsset method does not return a null.

It may be that the fromAsset method doesn't return null, but perhaps returns the same as defaultMarker() returns. (ie the documentation is wrong)

I'd check whether the value you get for a (presumably non-existent) asset is either '==' or .Equals the defaultMarker(). That could be how you detect a non-existent asset.

I have found, contrary to documentation, BitmapDescriptorFactory.fromAsset(String) does not return null if the specified asset does not exist. I am currently implementing a work around where I pre-load the list of assets which do exist using AssetManager.list(""). Then, for an asset name in question I search the cached list of assets to determine whether the specified asset exists or not.

This was the least resource intensive approach I could thnk of since I could not depend on a null return when the asset did not exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM