OAKLAND, Calif. — Every year, thousands of people addicted to opioids show up at hospital emergency rooms in withdrawal so agonizing it leaves them moaning and writhing on the floor. Usually, they’re given medicines that help with vomiting or diarrhea and sent on their way, maybe with a few numbers to call about treatment.

When Rhonda Hauswirth arrived at the Highland Hospital E.R. here, retching and shaking violently after a day and a half without heroin, something very different happened. She was offered a dose of buprenorphine on the spot. One of three medications approved in the United States to treat opioid addiction, it works by easing withdrawal symptoms and cravings. The tablet dissolved under her tongue while she slumped in a plastic chair, her long red hair obscuring her ashen face.

Soon, the shakes stopped. “I could focus a little more. I could see straight,” said Ms. Hauswirth, 40. “I’d never heard of anyone going to an emergency room to do that.”

Highland, a clattering big-city hospital where security wands constantly beep as new patients get scanned for weapons, is among a small group of institutions that have started initiating opioid addiction treatment in the E.R. Their aim is to plug a gaping hole in a medical system that consistently fails to provide treatment on demand, or any evidence-based treatment at all, even as more than two million Americans suffer from opioid addiction. According to the latest estimates, overdoses involving opioids killed nearly 50,000 people last year.