After requesting it for the backend, I want to display the product image to the user.
the problem is:
the API response with PNG image found the product image but if the product has no image the API response is with (204 NO Content), so I need to display the default image instead.
after searching I found the defaultSource
prop to the <Image>
component and it works fine on IOS but in android, it does not.
<Image
defaultSource={{ uri: productDefaultImage }}
source={{
uri: env.apiUrl.concat(env.productImageById.replace(":productId", price.id.toString())).concat("?thumbnail=true"), headers: { Authorization: `Bearer ${token}`}
}}
resizeMode="contain"
style={{ width: 100, height: 70 }}
onLoadEnd={() => setImageLoading(false)}
/>
some of the returned data from API
�PNG
IHDRww^�� pHYs��IDATx^����wu���9u��=9g͌rH ���1x�1��YG���kÚ�/�ؘl�,!��F��g:��ʹ�����ww�y�����6�����tW=ϭ�{���sϑ�;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;v���#`G���;?�p�d_�
}u?��2�sM8��~ޟ��;>WqZ��.���r>�8
]=���d6�Δ
}��ayB�B��.��|6,�j�dU��Ⱥ��z ����b>^�Y������S�ԂV&�w�r��j��@���7�>���5/��n>��D$���.o���x$���p8�t:�N����������rX��a5̇C!m4�a�T�Ѩ�U����
}������X���;��d�/?��")8j
9<N�5_i�>�����q:��W��uso��;��Gn�9�����?�I�V*�@�R�k
�,�'_s�sU��X����×/�ZҙLK�R�����>W��vW�Paf���4ꞈ�(����ɢ>���,+T�4¹����VP4ȧ�%o�w�Cmr�Z����9i~Z*f�I��3�*�C��T
ٜWV
]��=mr8�R*�GZ�BZ5��&q�R8$�ˣz.�j2)��>���K���H�
2�y4�A����|�&'��jr4�>�ިfT��V�5�?T=
}�J�������3�m�"����ɛ�{,����p���/g|]�wY����ov(�H��V[5������rqi�z>Xp���l �x}*y�H��ΒO�r�Z�NG�^��n�7���m�^�����{�����rj�5,��ҹe��;0�]�M�������B ���G�㎅�3X�T��r���v�*�Z#�u4�+nwЪYy�/P ��9�.��jX^�%ϫVq������獒�
is there’s any way I can display default image in android too?
2
Answers
If you move your uri into a piece of state then you can use the
onError
callback to set the uri to your default url ( a demo):