A Bar Harbor man who was caught by police with more than 500 bags of heroin and $28,000 in cash has been sentenced to four years in prison.