2013-02-21 132 views
0

我正在開發定製的相機應用程序。當應用程序啓動相機打開時,底部有三個按鈕。嘗試拍攝新照片時應用程序崩潰

  1. 捕獲(拍照)。
  2. 採取新的(從預覽返回到相機再次拍照。)
  3. 不使用任何東西。

一切工作正常。按下捕捉拍攝照片並精確預覽拍攝的照片。但是當我按下新按鈕進入相機模式從預覽模式應用程序崩潰不知道我在做什麼錯了。以下是我正在使用的代碼。

public class MainActivity extends Activity { 


    protected static final String TAG = null; 
    private Camera mCamera; 
    private CameraPreview mPreview; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    static int result; 
    static int degrees = 90; 
    private Button captureButton, btn_new; 
    public FrameLayout preview; 
    private static File mediaFile; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // 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); 
    captureButton = (Button) findViewById(R.id.button_capture); 
    captureButton.setOnClickListener(
     new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // get an image from the camera 
       mCamera.takePicture(null, null, mPicture); 
      } 
     } 
    ); 
    btn_new = (Button) findViewById(R.id.button_new); 
    btn_new.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      mCamera = getCameraInstance(); 

      //preview = (FrameLayout) findViewById(R.id.camera_preview); 
      //preview.addView(mPreview); 
     } 
    }); 

} 

private PictureCallback mPicture = new PictureCallback() { 

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

     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
     if (pictureFile == null){ 

      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()); 
     } 
    } 
}; 

/** Create a File for saving an image or video */ 
private static 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_DCIM), "Camera"); 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 

    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
     "IMG_"+ timeStamp + ".jpg"); 
    } 
    else { 
     return null; 
    } 

    return mediaFile; 
} 
@SuppressWarnings("null") 
public static Camera getCameraInstance(){ 
    Camera c = null; 
    Context context = null; 
    try{ 
     c = Camera.open(); 
     //setCameraDisplayOrientation(MainActivity, 0, c); 
     c.setDisplayOrientation(degrees); 
    } 
    catch (Exception e) { 
     Toast.makeText(context.getApplicationContext(),"Camera is not available" ,   Toast.LENGTH_LONG).show(); 

    } 
    return c; 
} 

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

}

在此先感謝。 最後 這裏是我的錯誤的logcat:


02-21 18:24:01.868:E/AndroidRuntime(937):致命異常:主要 02-21 18:24:01.868:E/AndroidRuntime( 937):java.lang.NullPointerException 02-21 18:24:01.868:E/AndroidRuntime(937):at com.example.facebooktag.MainActivity.getCameraInstance(MainActivity.java:136) 02-21 18:24: 01.868:E/AndroidRuntime(937):at com.example.facebooktag.MainActivity $ 3.onClick(MainActivity.java:66) 02-21 18:24:01.868:E/AndroidRuntime(937):at android.view.View .performClick(View.java:4202) 02-21 18:24:01.868:E/AndroidRuntime(937):at android.view .View $ PerformClick.run(View.java:17340) 02-21 18:24:01.868:E/AndroidRuntime(937):at android.os.Handler.handleCallback(Handler.java:725) 02-21 18 :24:01.868:E/AndroidRuntime(937):at android.os.Handler.dispatchMessage(Handler.java:92) 02-21 18:24:01.868:E/AndroidRuntime(937):at android.os.Looper .loop(Looper.java:137) 02-21 18:24:01.868:E/AndroidRuntime(937):at android.app.ActivityThread.main(ActivityThread.java:5039) 02-21 18:24:01.868 :E/AndroidRuntime(937):at java.lang.reflect.Method.invokeNative(Native Method) 02-21 18:24:01.868:E/AndroidRuntime(937):at java.lang.reflect.Method.invoke Method.java:511) 02-21 18:24:01.868:E/AndroidRuntime(937):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 02-21 18:24:01.868:E/AndroidRuntime(937):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-21 18:24:01.868:E/AndroidRuntime 937):at dalvik.system.NativeStart.main(Native Method)

+1

後logcat在這裏。 – 2013-02-21 17:12:38

+0

無法看到任何logcat有什麼建議嗎? – 2013-02-21 17:16:36

+0

您需要先讓LogCat工作。你在哪裏運行這個應用程序?如果它在沒有後置攝像頭或模擬器的設備上,則可能會從Camera.open()返回空值。因此,NullPointerException。 – 2013-02-21 17:21:03

回答

0

可能是因爲您已經第一次拍攝照片並獲取相機對象,然後您又在此處獲取相機對象。

下面的第一個實例setcontentview。

//創建相機

mCamera = getCameraInstance(); 

的實例,並試圖重新取得相機的實例,但是它已經收購。

btn_new = (Button) findViewById(R.id.button_new); 
btn_new.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     mCamera = getCameraInstance(); 

     //preview = (FrameLayout) findViewById(R.id.camera_preview); 
     //preview.addView(mPreview); 
    } 
}); 

在再次獲取相機對象之前,您應該先釋放第一個相機對象。

if(mCamera != null) 
    mCamera.release(); 

製作確保你有之前應用在標籤清單申報許可。

+0

我也試着先釋放相機。我將如何重用它? – 2013-02-21 17:26:37

+0

如果我刪除相機將如何打開(從預覽返回捕捉模式)? – 2013-02-21 17:32:16

+0

爲什麼釋放相機無法正常工作。 – 2013-02-21 17:33:29

0

那麼空指針異常來自你的異常。你有

Context context = null; 

,然後你正試圖在這裏使用它:

Toast.makeText(context.getApplicationContext(),"Camera is not available" ,   Toast.LENGTH_LONG).show(); 

沒有任何地方實際上可以初始化它。

同樣在你的onPause()函數中,你應該自己清理一下。 (例如,釋放相機,停止表面預覽,諸如此類的事情)。

相關問題