0

Hi I am writing a simple app that interacts with androids camera API directly, I'm using this tutorial http://www.tutorialspoint.com/android/android_camera.htm

Could anyone show me where I am going wrong?

The log output:

01-09 23:42:56.901: D/ATRecorder(886): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@40528bb8
01-09 23:42:57.161: D/AndroidRuntime(886): Shutting down VM
01-09 23:42:57.161: W/dalvikvm(886): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
01-09 23:42:57.171: E/AndroidRuntime(886): FATAL EXCEPTION: main
01-09 23:42:57.171: E/AndroidRuntime(886): java.lang.RuntimeException: startPreview failed
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.hardware.Camera.startPreview(Native Method)
01-09 23:42:57.171: E/AndroidRuntime(886):  at com.example.camera1.ShowCamera.surfaceCreated(ShowCamera.java:30)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.SurfaceView.updateWindow(SurfaceView.java:551)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.View.draw(View.java:6973)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.View.draw(View.java:6973)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-09 23:42:57.171: E/AndroidRuntime(886):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1997)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewRoot.draw(ViewRoot.java:1600)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1321)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.os.Looper.loop(Looper.java:150)
01-09 23:42:57.171: E/AndroidRuntime(886):  at android.app.ActivityThread.main(ActivityThread.java:4277)
01-09 23:42:57.171: E/AndroidRuntime(886):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 23:42:57.171: E/AndroidRuntime(886):  at java.lang.reflect.Method.invoke(Method.java:507)
01-09 23:42:57.171: E/AndroidRuntime(886):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 23:42:57.171: E/AndroidRuntime(886):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 23:42:57.171: E/AndroidRuntime(886):  at dalvik.system.NativeStart.main(Native Method)

I've followed it perfectly but I still get and uncaught exception in log-cat when I try to run it, here is the code in MainActivity:

package com.example.camera1;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

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) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
       }
    }

Here is the ShowCamera class:

package com.example.camera1;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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) {
   }

}
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
The_Neo
  • 308
  • 1
  • 4
  • 13

2 Answers2

0

Try to catch a top level Exception in the surfaceCreated method (instead of IOException) and call e.getMessage() in the exception block to get more information on the type of error.

Heisenberg
  • 56
  • 4
  • 1
    This is not really an answer, just a way to get to the answer. Use a comment for that. – M. Mimpen Jan 20 '14 at 10:26
  • It's a good advice to catch generic Exception and not only IOException working with camera. In this specific case, this could prevent the app crash (the author will need to design a fallback, though). But there will be no additional information not printed in the attached crash log. – Alex Cohn Jan 20 '14 at 10:36
  • Was supposed to be a comment but I needed a reputation of 50 to do so. – Heisenberg Jan 21 '14 at 09:30
0

There may be different reasons for Camera.startPreview() to throw RuntimeException. For example, it could be by set of preview size not to one of supported preview sizes.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307