An off-duty Long Island cop was hurt in a Bronx shooting during an attempted robbery early Saturday, police said.

The 33-year-old Hempstead officer was driving on Sedgwick Avenue near Hall of Fame Terrace in University Heights just after 6 a.m. when two men in a black Toyota Camry rear-ended him, cops said.

As the victim and the Toyota occupants got out of their cars to assess the damage, a red Nissan approached, and two other men got out, police said.

The four men then began assaulting the officer in an attempt to rob him, cops said.

During the struggle, the officer fired his gun, striking a 19-year-old man in the stomach and a 24-year-old man in the back, cops said.

At some point, the officer was also shot in the right calf. Cops are investigating whether he inadvertently shot himself.

Two of the suspects drove their wounded associates to Bronx Lebanon Hospital, then fled.

The injured pair were taken into custody, police said. Both remained at the hospital in stable condition, with charges pending late Saturday morning.

The officer was taken to St. Barnabas Medical Center in stable condition.