相机getParameters失败

时间:2014-03-15 20:02:16

标签: android camera

该代码在三星手机上正常运行。但手机HTC ONE和许多其他手机应用程序在调用以下方法时崩溃了.getParameters()

日志:

java.lang.RuntimeException: getParameters failed (empty parameters)
at android.hardware.Camera.native_getParameters(Native Method)
at android.hardware.Camera.getParameters(Camera.java:1460)
at com.bumsun.trollface.MainActivity$8.onCheckedChanged(MainActivity.java:136)
at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
at android.widget.Switch.setChecked(Switch.java:688)
at android.widget.Switch.animateThumbToCheckedState(Switch.java:679)
at android.widget.Switch.stopDrag(Switch.java:669)
at android.widget.Switch.onTouchEvent(Switch.java:629)
at android.view.View.dispatchTouchEvent(View.java:7127)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
at android.view.View.dispatchPointerEvent(View.java:7307)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4157)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4136)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4228)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:125)
at android.os.Looper.loop(Looper.java:124)
at android.app.ActivityThread.main(ActivityThread.java:4800)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

if (mCamera == null) 
    return; //don't write anything between these two lines, previously I got this error, because I put a log out.
Camera.Parameters lParameters = mCamera.getParameters();

答案 1 :(得分:0)

我只是得到一个与你相同的问题。我查了一下,发现相机被调用解锁()  所以我在getParameter之前添加了lock()并在setParameter之后添加了unlock()。希望它可以帮到你!

//there is neeed try catch block
if(mMediaRecorder == null) {
            mCamera.lock();
            mParameters = mCamera.getParameters();
        }else {
            mParameters = mCamera.getParameters();
        }

if(mParameters != null && mCamera != null){
    final String mode = mParameters.getFlashMode();
    try{
        if (TextUtils.isEmpty(mode) || Camera.Parameters.FLASH_MODE_OFF.equals(mode))
        {
            mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        }else {
            mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        }
        mCamera.setParameters(mParameters);
    }catch(Exception ex) {
        ex.printStackTrace();
    }finally {
        try{
            mCamera.unlock();
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}