使用" Camera API"时会出现致命异常

时间:2014-04-18 09:49:02

标签: android camera android-camera

我是Android的新手,我正在尝试使用 Android默认的Camera API 来按照教程实现示例应用程序。

实施时我得到致命异常

代码如下::

主要Activity.java

public class MainActivity extends Activity {


private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
public static Camera isCameraAvailiable(){
   Camera object = null;
      try {
         object = Camera.open(); 
      }
      catch (Exception e){
      }
      return object; 
   }

   private PictureCallback capturedIt = new PictureCallback() {

          @Override
          public void onPictureTaken(byte[] data, Camera camera) {

          Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
          if(bitmap==null){
             Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
          }
          else
          {
             Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();       
          }
          cameraObject.release();
       }
    };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); pic = (ImageView)findViewById(R.id.imageView1);
      cameraObject = isCameraAvailiable();
      showCamera = new ShowCamera(this, cameraObject);
      FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
      preview.addView(showCamera);
   }
   public void snapIt(View view){
      cameraObject.takePicture(null, null, capturedIt);
   }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

ShowCamera.java

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

   private SurfaceHolder holdMe;
   private Camera theCamera;

   public ShowCamera(Context context,Camera camera) {
      super(context);
      theCamera = camera;
      holdMe = getHolder();
      holdMe.addCallback(this);
   }

   @Override
   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
      try   {
         theCamera.setPreviewDisplay(holder);
         theCamera.startPreview(); 
      } catch (IOException e) {
      }
   }

   @Override
   public void surfaceDestroyed(SurfaceHolder arg0) {
   }

}

异常::

04-18 13:23:35.835: E/AndroidRuntime(13240): FATAL EXCEPTION: main
04-18 13:23:35.835: E/AndroidRuntime(13240): java.lang.RuntimeException: startPreview failed
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.hardware.Camera.startPreview(Native Method)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at com.example.cameraapi.ShowCamera.surfaceCreated(ShowCamera.java:29)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.SurfaceView.updateWindow(SurfaceView.java:552)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.View.draw(View.java:6883)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.View.draw(View.java:6883)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1940)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewRoot.draw(ViewRoot.java:1527)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1264)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.os.Looper.loop(Looper.java:130)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at java.lang.reflect.Method.invoke(Method.java:507)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-18 13:23:35.835: E/AndroidRuntime(13240):    at dalvik.system.NativeStart.main(Native Method)

新例外::

04-18 15:33:24.234: W/dalvikvm(16939): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-18 15:33:24.359: E/AndroidRuntime(16939): FATAL EXCEPTION: main
04-18 15:33:24.359: E/AndroidRuntime(16939): java.lang.IllegalStateException: Could not execute method of the activity
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.view.View$1.onClick(View.java:2144)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.view.View.performClick(View.java:2485)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.view.View$PerformClick.run(View.java:9080)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.os.Handler.handleCallback(Handler.java:587)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.os.Looper.loop(Looper.java:130)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at java.lang.reflect.Method.invoke(Method.java:507)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at dalvik.system.NativeStart.main(Native Method)
04-18 15:33:24.359: E/AndroidRuntime(16939): Caused by: java.lang.reflect.InvocationTargetException
04-18 15:33:24.359: E/AndroidRuntime(16939):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at java.lang.reflect.Method.invoke(Method.java:507)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.view.View$1.onClick(View.java:2139)
04-18 15:33:24.359: E/AndroidRuntime(16939):    ... 11 more
04-18 15:33:24.359: E/AndroidRuntime(16939): Caused by: java.lang.RuntimeException: Method called after release()
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.hardware.Camera.native_takePicture(Native Method)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.hardware.Camera.takePicture(Camera.java:878)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at android.hardware.Camera.takePicture(Camera.java:842)
04-18 15:33:24.359: E/AndroidRuntime(16939):    at com.example.cameraapi.MainActivity.snapIt(MainActivity.java:57)
04-18 15:33:24.359: E/AndroidRuntime(16939):    ... 14 more

1 个答案:

答案 0 :(得分:0)

我不确定,但似乎您没有为SurfaceHolder设置类型。尝试在setType方法中调用ShowCamera方法,如下所示:

holdMe.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);