How to get the image URL?

Hello,

I cannot get the image URL - I tried buildingImage.fields.file.url, and so forth but I keep getting back null. Please see the image attached. buildingImage is a linked image asset. It is returned in the query but I cannot get the URL from the response.

Also, for location, it was being returned as text before then suddenly it’s only returning lat/long. Can I get a string? Or do I have to use GeoCoder?

1 Like

Hey,

to answer your question fully, do you mind sharing what type buildingImage is? If it is a CDAEntry, with a field linking the asset, something like this might be working:

val asset = result.getField<CDAAsset>("media")
val url = asset.url()

Please see that the media field of an entry links to an asset. if you have an asset, you can ask the .url() method to get the protocol less url. You would need to add https: to it to make it a complete url.

To answer your second question: You cannot get the location as a string. The location object is the only one supported. You would need to get the field and then call use it as a map to get its “lat” or “lon” value in Double.

Greetings,
Mario

Hwy Mario,

Thanks for your reponse. buildingImage is an Image type:

Much appreciated got the Image working.

For the location, is there an example to convert lat/long into an actual string address (I would have to use Geocoder)?

Hey,

with the given information I can only wager to think that you are using the android geocoder, please refer to their documentation on how to use it.

You can get the latitude and longitude from the field of the Contentful object given, by using its lat and lon entries.

Greetings,
Mario.

Hey, Mario. I want to ask a further question. If in contentful model, we set the image field as optional. And in BE codes, how do get this image url? I mean mainly the return type of the image, like do we need to declare that the CDAAssets is nullable? like this one below:
item.getField<**CDAAsset?**>("image")?.urlForImageWith(ImageOption.https())
Or the CDAAssets cannot be used as nullable because it is not basic types like Double or int