A man found in possession of cannabis at Ninewells Hospital later threatened police officers who arrived to investigate the discovery.
Jamie Cormie, 25, of Glenmoy Terrace, Forfar, admitted having the Class B drug on February 13.
He further admitted that in the early hours of the following morning at Ninewells, he behaved in a threatening or abusive manner in that he shouted and swore at police officers.
Dundee Sheriff Court heard Cormie was admitted to hospital at 7pm after a drug-related incident and, when staff were changing him three hours later, a piece of paper containing brown powder fell from his boxer shorts.
Police were called and when they arrived and spoke to him he began shouting and swearing and saying he could not go back to prison.
Despite being told to calm down by officers he continued swearing and was arrested.
Solicitor Sarah Russo said Cormie had just been released from prison and was afraid he was going to be sent back.
He had been engaging with the Tayside Substance Misuse Service and wanted to carry on working with the agency.
Sheriff Lorna Drummond deferred sentence on Cormie until July 17 for reports.
He was granted bail meantime.