A 48-year-old man was charged Wednesday, a day after he shot himself in the leg at Zabar's on the Upper West Side, police said.

The man, Louis Anderson, was adjusting a gun from his right pocket to his left after getting his bagel and coffee when the gun went off at about 8:40 a.m. on Tuesday, according to police and court records.

Anderson, who also goes by the name Leston, then ran out of the popular eatery, wrapped the 9 mm. in a plastic bag and put it next to a rock in Riverside Park, near 79th Street, according to the criminal complaint.

He was charged Wednesday with criminal possession of a weapon and reckless endangerment, police said.

He was held on $15,000 bond and is expected back in court on June 20. An attorney for Anderson could not immediately be reached for comment.

He told investigators he found the gun in cardboard while rummaging through garbage near the Lincoln Tunnel.

He was able to get himself to Mount Sinai St. Luke’s Hospital, where police were called.

Anderson is homeless and has nine prior arrests, several for drug-related offenses, police said.