应用程序在构建自定义相机时崩溃

时间:2013-11-26 07:06:58

标签: android android-camera surfaceview

单击该按钮捕获图像时应用程序崩溃。请帮助。下面是我的代码。我使用SurfaceView进行预览。我猜图片回调存在问题

  public class MainActivity extends Activity {

   private Camera mCamera;
   private CameraPreview mPreview;
   public static final int MEDIA_TYPE_IMAGE = 1;


   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
    Button captureButton = (Button) findViewById(R.id.button_capture);
   System.out.println("Starting!");

   // Create an instance of Camera
   mCamera = getCameraInstance();
  // Create our Preview view and set it as the content of our activity.
   mPreview = new CameraPreview(this, mCamera);
   FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
   preview.addView(mPreview);

    final PictureCallback mPicture = new PictureCallback() {

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

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d("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("TAG", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("TAG", "Error accessing file: " + e.getMessage());
        }
       }
       };




      // Add a listener to the Capture button
       captureButton.setOnClickListener(

        new View.OnClickListener() {

            public void onClick(View v) {
                // get an image from the camera   

                System.out.println("Photo Taking!");
                mCamera.takePicture(null, null, mPicture);
            }
         }
    );
     }

/ **获取Camera对象实例的安全方法。 * /

       public static Camera getCameraInstance(){
        Camera c = null;
         try {
         c = Camera.open(); // attempt to get a Camera instance
          }
        catch (Exception e){
      // Camera is not available (in use or does not exist)
    }
     return c; // returns null if camera is unavailable
   }

 @Override
 protected void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
    }



   private void releaseCamera(){
     if (mCamera != null){
       mCamera.release();        // release the camera for other applications
       mCamera = null;
    }
}
   /** Create a File for saving an image or video */
     private  File getOutputMediaFile(int type){
   // To be safe, you should check that the SDCard is mounted
   // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
          Environment.DIRECTORY_PICTURES), "MyCameraApp");


  // This location works best if you want the created images to be shared
  // between applications and persist after your app has been uninstalled.

  // Create the storage directory if it does not exist
  if (! mediaStorageDir.exists()){
    if (! mediaStorageDir.mkdirs()){
        return null;
    }
   }

   // Create a media file name
  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  File mediaFile;
  if (type == MEDIA_TYPE_IMAGE){
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "IMG_"+ timeStamp + ".jpg");
   } else {
    return null;
   }

  return mediaFile;
   }
  }

Logcar错误:

java.lang.NullPointerException
com.example.demo.MainActivity$2.onClick(MainActivity.java:82)
android.view.View.performClick(View.java:4084)
android.view.View$PerformClick.run(View.java:16966)
android.os.Handler.handleCallback(Handler.java:615)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4745)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案