Can you just take a tent, take some food and stay for a few days in national park in Sri Lanka? First of all is it legal to do so? For example are campfires allowed? And if it is legal is it safe? Are there any dangerous animals?
There are quite a few safari camping trips, or organised trekking tours - and they mention for example, that "Wilderness camping sites may include pitching tent in a cave or under a rock face, by the lakeside or high on the plateau. ".
They also suggest "the indigenous Vadda people are always helpful to campers with the Vadda village being an interesting area to camp".
So that would seem to imply that it's acceptable and that the locals are ok with you doing it. However, this could still be because the company has a permit or something that they don't mention.
Sri Lanka Eco Tourism mentions that they have "Camping rules to ensure safe and comfortable camping and Wildlife and/or Forest Department regulations, when applicable".
So it would seem that it's best to identify which national park you want to camp in (Yala, Bundala, etc) and then contact the Forest Department for that park, to find out exactly what is required.
As for the dangerous animals - yes, in parts. In Yala National Park, for example, there are crocodiles hidden everywhere, dangerous lizards, and heaps of other troublesome critters.
It seems the answer would be yes.
Wikitravel's entry on Yala National Park mentions that a day entry pass fee costs LKR 3500, which probably includes camping. I don't find an official government website listing rules, but there are many tour operator pages which mention camping. Yala Hotels (at Yala National Park) says their tour package includes (my emphasis):
This suggests that camping is allowed. What you need to check - and I don't know whether this is your intention - is whether camping is allowed for non-organised parties. I would reckon the answer to be no; with illegal poaching in the Indian subcontinent, forest departments usually tend to keep human activity restricted. Indigenous people could be allowed to live there but there may be restrictions on travellers.
This answer is for my assumption that you want to go camping on your own/with friends (without guides and porters), and this comes from someone who has camped and hitchhiked a lot in a country where relatively very few people do so.
1) Camping in national parks is strictly illegal unless done in a designated campsite after paying authorities. If caught illegally camping, you will be subject to a hefty fine and sometimes imprisonment or even both. Unlike the police in Sri Lanka, wildlife officials cannot be (easily) bribed, so don't expect to get away easily.
2) To book a designated campsite, you can book them from the wildlife department office at Battaramulla, Colombo in advance. You can get a travel agent or your hotel to do this for you as you have to book in advance, especially during the holiday seasons.
3) It is useless camping in the dry zone of the island for numerous reasons. It is dry and hot with few sources of water if you don't camp by a river. Also, if an elephant doesn't trample you, a bear, leopard or croc might eat you for dinner. Even if that doesn't happen, there many venomous snakes around that could kill you. However, the eastern part (also in the dry zone) of the country is a nice place for camping, however, if you are not a local, I advice you not to, and I have put my reasons in this answer.
4) The wet zone of the country is more suited to camping, and the highlands are the best. Try Horton plains national park (they have designated campsites) or you can legally camp along Belihul oya or Hirikatu oya. If you need additional info, I can help you in this regard. Just open a new question and let me know. Camping is also possible in Sinharaja and knuckles reserves. Although non-existent in altitudes over 1500m, leeches are the biggest nuisance when camping in the lower highlands (This is the case in Sinharaja). Mosquitoes may also be an issue. However in the higher altitudes, there less of these, and also lesser snakes and other venomous animals. Leopards are found in the highlands as well but in Sri Lanka they are not known to attack humans and they are quite rare. So if you want to just pick up your bags and go camping, one of the above maybe your choice.
5) It is illegal to light bonfires and you should not do that. In dry periods, forests even in the hills can catch fire and this happens quite regularly. So make sure you douse out any small fire you make to cook food.
6) Camping in Sri Lanka is very safe as long as you keep within the law. There is more danger from nature than from people. Never ever camp near streams or rivers in the highlands as water levels can change very quickly in a matter of minutes. Keep your tent closed at night and if some animal such as Sambhur or wild boar approach at night, keep silent and relaxed. Do not try to feed them. In general, Sri Lanka is a very safe country and you should not have to worry about anything if you excise common sense.