German naval landing parties wore straw hats to begin with, but they went out of fashion in the 1880s and pith helmets became the order of the day. On board ship they would have worn soft naval caps.
Your chaps in the second photo are from the SMS Königsberg, and during their adventure uniform regulations went out of the window. They firstly began dying their uniforms a light khaki colour, and as time went on they eventually became indistinguishable from their Schutztruppe comrades. They would have replaced lost items with anything they could get their hands on. Those could well be civilian hats or hats obtained from the Schutztruppe. Or I suppose they might have still had some kicking about in a cupboard somewhere. Point is, regulations had stopped dictating what the seamen wore by that point.
In stark contrast, photos of the landing party from the Emden in 1914 show them in immaculate matching uniforms, and every man jack of them is wearing a sun helmet. Officers wearing white ones and the men wearing khaki ones.
The first photo shows a German naval Maxim crew in Samoa rather than Africa, in the 1880s. Clearly straw hats were still in vogue in the South Seas at that date.
Edited for typo.