I\'m trying to build a custom camera app. I have a class which extends SurfaceView for the camera preview. When the device changes orientation, this surface is destroyed and
If your manifest does not declare that the activity handles orientation changes, and is not locked to a single orientation, then rotation of the device will cause the whole activity to get unloaded and reloaded again. Isn't this what you experience now?
If you specify android:configChanges="orientation|screenSize"
for your activity, then the user experience will be much smoother. But still, you cannot keep the camera preview "intact" after such rotation; the layout will be rebuilt.
Finally, the approach of the stock camera app is to set fixed (usually landscape) activity orientation and fake the portrait orientation by changing the buttons and other UI elements. But if you look for the system notifications or navigation (soft) buttons on the bottom, you will understand that actually device is locked in landscape, only controls are redrawn. You will also notice that camera apps avoid standard text widgets, because such widgets cannot be easily rotated.
By the way, there is another misunderstanding in your question. If you want to store the picture as portrait, you will need (or not need) manual rotation of the JPEG regardless of your activity orientation. On most devices, camera can only produce landscape JPEG files. Camera API includes setRotation() method, but usually, this only sets an EXIF flag in the JPEG header. Not all image viewers respect this flag. You may need manual rotation of the image if you don't want to compromize.