This is all unused, right?  Did you get this from the camera app?  That one locks its orientation and manages it manually. But gallery is allowed to rotate, so doesn't need code like this.