问题
I am opening front camera in my app. on the first time app run ok but when I run app again 2nd, 3rd or 4th times, it gives me exception java.lang.RuntimeException : set display orientation failed
.
Help Please.
public class MirrorView extends SurfaceView implements
SurfaceHolder.Callback
{
private static SurfaceHolder mHolder;
private static Camera mCamera;
private int zoom;
private boolean freeze, hide = false;
private final static String DEBUG_TAG = "MirrorView";
public static final int MEDIA_TYPE_IMAGE = 1;
public MirrorView(Context context, Camera camera)
{
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder)
{
try
{
mCamera.stopPreview();
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
Log.v(DEBUG_TAG, "Surface Created");
}
catch (Exception error)
{
Log.d("Debug",
"Error starting mPreviewLayout: " + error.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Log.v(DEBUG_TAG, "Surface changed");
if (mHolder.getSurface() == null)
{
Log.v(DEBUG_TAG, "Holder is null");
return;
}
// can't make changes while mPreviewLayout is active
try
{
mCamera.stopPreview();
Log.v(DEBUG_TAG, "Preview Stopped");
}
catch (Exception e)
{
e.printStackTrace();
}
Display display = Main.getDisplay();
Camera.Parameters parameters = mCamera.getParameters();
if(display.getRotation() == Surface.ROTATION_0)
{
Log.v(DEBUG_TAG, "orientation set to 90");
parameters.setPreviewSize(height, width);
mCamera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(width, height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
Log.v(DEBUG_TAG, "orientation set to 180");
parameters.setPreviewSize(width, height);
mCamera.setDisplayOrientation(180);
}
try
{
// start up the mPreviewLayout
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch (Exception error)
{
Log.d("Debug",
"Error starting mPreviewLayout: " + error.getMessage());
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN)
{
if(hide)
{
Main.ShowViews();
hide = false;
}
else
{
Main.HideShow();
hide = true;
}
}
return true;
}
/*private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = null; //getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null)
{
Log.d(DEBUG_TAG, "Error creating media file, check storage permissions: " +
"");
return;
}
try {
// FileOutputStream fos = new FileOutputStream(pictureFile);
// fos.write(data);
// fos.close();
} catch (FileNotFoundException e) {
Log.d(DEBUG_TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(DEBUG_TAG, "Error accessing file: " + e.getMessage());
}
}
};*/
public void ZoomIn()
{
Camera.Parameters p = mCamera.getParameters();
int maxZoom = p.getMaxZoom();
if (p.isZoomSupported())
{
if (zoom < maxZoom)
{
zoom += 10;
}
p.setZoom(zoom);
}
mCamera.setParameters(p);
try
{
mCamera.setPreviewDisplay(mHolder);
}
catch (Exception e)
{
e.printStackTrace();
}
mCamera.startPreview();
}
public void ZoomOut()
{
Camera.Parameters p = mCamera.getParameters();
if (p.isZoomSupported())
{
if (zoom > 0)
{
zoom -= 10;
}
p.setZoom(zoom);
}
mCamera.setParameters(p);
try
{
mCamera.setPreviewDisplay(mHolder);
}
catch (Exception e)
{
e.printStackTrace();
}
mCamera.startPreview();
}
public void freezeMirror()
{
if(freeze)
{
try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
freeze = false;
}
else
{
// mCamera.takePicture(null, null, mPicture);
mCamera.stopPreview();
mCamera.startPreview();
freeze = true;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if(mCamera != null)
{
//mCamera.stopPreview();
mCamera.release();
}
}
}
and I have main class which is my laucher Activity
public class Main extends Activity
{
private Camera mCam;
private MirrorView mCamPreview;
private int mCameraId = 0;
private FrameLayout mPreviewLayout;
private static ImageView zoomin, zoomout, freeze;
private static Display display;
private static File picfile;
private static final String DEBUG_TAG = "Main";
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCameraId = findFirstFrontFacingCamera();
mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
zoomin = (ImageView) findViewById(R.id.zoomin);
zoomout = (ImageView) findViewById(R.id.zoomout);
// freeze = (ImageView) findViewById(R.id.freeze);
mPreviewLayout.removeAllViews();
display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// picfile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
setListeners();
startCameraInLayout(mPreviewLayout, mCameraId);
}
public static void ShowViews()
{
zoomin.setVisibility(View.VISIBLE);
zoomout.setVisibility(View.VISIBLE);
//freeze.setVisibility(View.VISIBLE);
}
public static void HideShow()
{
zoomin.setVisibility(View.INVISIBLE);
zoomout.setVisibility(View.INVISIBLE);
//freeze.setVisibility(View.INVISIBLE);
}
public static Display getDisplay()
{
return display;
}
public void setListeners()
{
zoomin.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
mCamPreview.ZoomIn();
}
});
zoomout.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
mCamPreview.ZoomOut();
}
});
/* freeze.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
mCamPreview.freezeMirror();
}
});*/
}
@Override
protected void onPause()
{
super.onPause();
Log.v(DEBUG_TAG, "Pause");
freeCamera();
}
public void freeCamera()
{
if (mCam != null)
{
mCam.release();
mCam = null;
}
}
private int findFirstFrontFacingCamera()
{
int foundId = -1;
int numCams = Camera.getNumberOfCameras();
for (int camId = 0; camId < numCams; camId++)
{
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(camId, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT)
{
foundId = camId;
break;
}
}
return foundId;
}
private void startCameraInLayout(FrameLayout layout, int cameraId)
{
mCam = Camera.open(cameraId);
if (mCam != null)
{
mCamPreview = new MirrorView(this, mCam);
// setCameraDisplayOrientationAndSize();
layout.addView(mCamPreview);
}
}
}
Log file:
11-28 11:05:23.729: E/AndroidRuntime(23827): FATAL EXCEPTION: main
11-28 11:05:23.729: E/AndroidRuntime(23827): java.lang.RuntimeException: set display orientation failed
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.hardware.Camera.setDisplayOrientation(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.gsmappstabs.mirror.MirrorView.surfaceChanged(MirrorView.java:83)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1869)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewRoot.draw(ViewRoot.java:1540)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewRoot.performTraversals(ViewRoot.java:1276)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.os.Looper.loop(Looper.java:130)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.app.ActivityThread.main(ActivityThread.java:3701)
11-28 11:05:23.729: E/AndroidRuntime(23827): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827): at java.lang.reflect.Method.invoke(Method.java:507)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
11-28 11:05:23.729: E/AndroidRuntime(23827): at dalvik.system.NativeStart.main(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827): [Blue Error Handler] Make Debugging Report file for main
11-28 11:05:23.729: E/AndroidRuntime(23827): java.lang.RuntimeException: set display orientation failed
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.hardware.Camera.setDisplayOrientation(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.gsmappstabs.mirror.MirrorView.surfaceChanged(MirrorView.java:83)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1869)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewRoot.draw(ViewRoot.java:1540)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewRoot.performTraversals(ViewRoot.java:1276)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.os.Looper.loop(Looper.java:130)
11-28 11:05:23.729: E/AndroidRuntime(23827): at android.app.ActivityThread.main(ActivityThread.java:3701)
11-28 11:05:23.729: E/AndroidRuntime(23827): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827): at java.lang.reflect.Method.invoke(Method.java:507)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
11-28 11:05:23.729: E/AndroidRuntime(23827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
11-28 11:05:23.729: E/AndroidRuntime(23827): at dalvik.system.NativeStart.main(Native Method)
回答1:
Call stopPreview()
before setting the display orientation, and then call startPreview()
after setting the display orientation.
mCamera.stopPreview();
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
回答2:
Though it is an old question and solution might be available on other questions. But here is my solution for future reference,
Make sure to stop preview and release camera on activity pause.
@Override
protected void onPause()
{
super.onPause();
Log.v(DEBUG_TAG, "Pause");
mCam.stopPreview();
mCam.release();
}
来源:https://stackoverflow.com/questions/20259063/set-display-orientation-failed