2016-03-10 162 views
-3

我正在做一個camara項目,其中一個活動捕獲的圖像被髮送到另一個活動。問題是,當我在拍攝按鈕,單擊該應用程序崩潰,並顯示在Camara應用程序崩潰點擊

public class MainActivity extends Activity { 

    private Camera mCamera; 
    private CameraPreview mPreview; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mCamera = getCameraInstance(); 



     // Create the Camera preview view 
     mPreview = new CameraPreview(this, mCamera); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
     preview.addView(mPreview); 

     final Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

      @Override 
      public void onPictureTaken(byte[] data, Camera camera) { 
       Intent intent = new Intent(MainActivity.this, EditActivity.class); 
       intent.putExtra(EditActivity.EXTRA_BYTES, data); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
      } 
     }; 

     // Add a listener to the capture button 
     ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture); 
     captureButton.setImageDrawable(getResources().getDrawable(R.drawable.cam)); 
     captureButton.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         mCamera.takePicture(null, null, mPicture); 
        } 
       } 

     ); 


    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     finish(); 
     mPreview.stopPreview(); 
    } 

    /** 
    * A safe way to get an instance of the Camera object. 
    * 
    * Returns null if camera is unavailable. 
    */ 
    private static Camera getCameraInstance() { 
     Camera c = null; 
     try { 
      c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 
     } catch (Exception ignore) { 
      // Camera is not available (in use or does not exist). Do nothing. 
     } 
     return c; 
    } 

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 

     private SurfaceHolder mHolder; 
     private Camera mCamera; 

     public CameraPreview(Context context, Camera camera) { 
      super(context); 
      mCamera = camera; 

      // Install a SurfaceHolder.Callback so we get notified when the 
      // underlying surface is created and destroyed. 
      mHolder = getHolder(); 
      mHolder.addCallback(this); 
      // deprecated setting, but required on Android versions prior to 3.0 
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     } 

     public void surfaceCreated(SurfaceHolder holder) { 
      // The Surface has been created, now tell the camera where to draw the preview. 
      try { 
       if (mCamera != null) { 
        mCamera.setDisplayOrientation(90); 
        mCamera.setPreviewDisplay(holder); 
        mCamera.startPreview(); 
       } 
      } catch (IOException ignore) { 
       // Do nothing 
      } 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 
      if (mCamera != null) { 
       mCamera.stopPreview(); 
      } 
     } 

     private void stopPreview() { 
      if (mCamera != null) { 
       mCamera.stopPreview(); 
       mCamera.release(); 
       mCamera = null; 
      } 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
      // If your preview can change or rotate, take care of those events here. 
      // Make sure to stop the preview before resizing or reformatting it. 

      if (mHolder.getSurface() == null) { 
       // preview surface does not exist 
       return; 
      } 

      // Stop preview before making changes 
      try { 
       mCamera.stopPreview(); 
      } catch (Exception ignore) { 
       // Tried to stop a non-existent preview. Do nothing. 
      } 

      // Set preview size and make any resize, rotate or reformatting changes here 

      // Start preview with new settings 
      try { 
       mCamera.setPreviewDisplay(mHolder); 
       mCamera.startPreview(); 
      } catch (Exception ignore) { 
       // Do nothing 
      } 
     } 
    } 
} 
+0

請提供堆棧跟蹤和確切的異常 – NightFury

+0

「_and在logcat_上顯示空值點異常」當在此處提問時,最好_實際顯示LogCat和堆棧跟蹤。 – JonasCz

回答

1

除非你選擇的圖像分辨率低,你是一個交易過大的異常崩潰的logcat的零點例外。 IPC交易規模有1MB限制,這意味着您的Intent的規模限制爲1MB。大多數相機的照片將遠遠大於此。

的選項有:

  1. 以低分辨率圖像

  2. 擺脫次活動,並完成所有的工作,在一個活動(例如,使用多個片段)

  3. 小心通過一些其他方式(例如,單身)從第一個活動獲取圖像,採取適當的步驟避免討厭的內存泄漏

0

這是部分在您的代碼崩潰:

   @Override 
       public void onClick(View v) { 
        mCamera.takePicture(null, null, mPicture); 
       } 

讓我們來看看它在哪裏可以從何而來。你有四個值:

  • mCamera:如果mCameranull,然後NullPointerException被拋出,因爲你嘗試使用unexisting對象
  • 第一和第二個參數是nulltakePicture方法。如果takePicture試圖使用這些參數的數據成員或方法,則引發NullPointerException
  • mPicture可能是null。如果mPicturenull並嘗試使用其數據成員/方法中的一種內takePicture,然後NullPointerException拋出

我相信mCameranull。如果我是正確的,你應該檢討你如何建立你的相機對象:

/** 
* A safe way to get an instance of the Camera object. 
* 
* Returns null if camera is unavailable. 
*/ 
private static Camera getCameraInstance() { 
    Camera c = null; 
    try { 
     c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 
    } catch (Exception ignore) { 
     // Camera is not available (in use or does not exist). Do nothing. 
    } 
    return c; 
} 

在這裏,你隱藏任何Exception。我想懇請您調試您的項目,看看這裏是否有Exception。如果你看到給定的Exception,你很可能會意識到你的問題是什麼。

+0

耶正是那裏的應用程序崩潰。解決辦法是什麼? – Yash

+0

@Yash,解決方案是找出導致NullPointerException的原因。執行時mCamera爲空嗎?如果是這樣,找出忽略的性質(你忽略的例外) –

相關問題