Could a space "ark" be built that has foliage and an entire ecosystem inside of it? Could it gain energy from the cosmic radiation throughout the galaxy?
What that looks like depends on the intended payload and destination and such, but on basically every variation, the answers are most certainly and sorta.
For certain scales, that would describe Earth; which is obviously capable of supporting entire ecosystems.
Smaller than that, and you get
O'Neill cylinders and
Stanford Torus.
Larger, and you get ringworlds.
For the most part, those are intended for habitation near stellar bodies (though if you're constructing a ringworld, you probably have the ability to move or construct stars).
In a stellar environment, you would have all the energy you need, even at relatively large distances, so long as you could collect it efficiently; though that obviously gets more cost prohibitive the farther out you're staying.
From there, it comes down to an energy balance: how much energy is necessary to sustain the amount of life being transported. Certain extremophiles would probably be just fine in interstellar space if they could survive dormancy for very long periods of time; certain bacteria can apparently survive on electricity alone. In deep space, cosmic radiation will give you approximately as much energy as you would get from solar panels collecting the starlight in interstellar space; 10
-13 Joules / cubic meter, or approximately 10
-5 Joules per second per cubic meter, since it is travelling at roughly the speed of light, and assuming 100% collection efficiency and that the collectors don't obstruct one another. So without some other source of energy, you wouldn't be able to sustain any sort of large, growing, or mobile creatures without pretty extensive methods of harnessing the energy from cosmic radiation (extensive in this case meaning basically planet-sized collecters). Humans, for example, require around 5-10MJ of energy in food per day, or around 3600MJ/year. All in all, it means humans are essentially 100 watt lightbulbs.
However, luckily, we have other sources as well. Uranium and thorium both have around 80,000,000MJ/kg
according to wiki, which is quite a good number of lightbulbs. While there's certainly going to be a lot of inefficiency in converting that into a form usable by life (unless you're using the aforementioned electricity-consuming bacteria), so you can sustain quite a decent amount of life for quite a decent amount of time using a relatively small stockpile of nuclear fuel.
Note: Do not take these figures as gospel. Except the bit about humans being 100 watt lightbulbs. Humans should be compared to lightbulbs as often as possible in your daily life.