2013-07-17 98 views
0

我在使用相機api在Android中拍照時遇到問題。使用相機意圖似乎工作正常,但不是當我直接調用API。安卓相機使用相機api時失敗

錯誤: java.lang.RuntimeException: takePicture failed at android.hardware.Camera.native_takePicture

代碼

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    cameraId = findFrontFacingCamera(); 
    camera = Camera.open(cameraId); 
    Parameters params = camera.getParameters(); 

    SurfaceView dummy=new SurfaceView(context); 
    try { 
     camera.setPreviewDisplay(dummy.getHolder()); 
     camera.startPreview(); 
     camera.takePicture(null, photoHandler, photoHandler); 

    } catch (IOException e) { 
     camera.stopPreview(); 
     camera.release(); 
    } 
} 

private PictureCallback photoHandler = new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
     } 
} 

清單

<uses-permission android:name="android.permission.CAMERA"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
<uses-feature android:name="android.hardware.camera"/> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 

日誌貓輸出

07-17 10:16:02.523: D/MakePhotoActivity(14591): Camera found 
07-17 10:16:02.773: D/dalvikvm(14591): threadid=1: still suspended after undo (sc=1 dc=1) 
07-17 10:16:18.229: D/AndroidRuntime(14591): Shutting down VM 
07-17 10:16:18.229: W/dalvikvm(14591): threadid=1: thread exiting with uncaught exception (group=0x40c4f930) 
07-17 10:16:18.309: E/AndroidRuntime(14591): FATAL EXCEPTION: main 
07-17 10:16:18.309: E/AndroidRuntime(14591): java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.fakelove.stationtostation/tv.fakelove.stationtostation.MainActivity}: java.lang.RuntimeException: takePicture failed 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.os.Looper.loop(Looper.java:137) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at java.lang.reflect.Method.invokeNative(Native Method) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at java.lang.reflect.Method.invoke(Method.java:511) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at dalvik.system.NativeStart.main(Native Method) 
07-17 10:16:18.309: E/AndroidRuntime(14591): Caused by: java.lang.RuntimeException: takePicture failed 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.native_takePicture(Native Method) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.takePicture(Camera.java:1095) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.takePicture(Camera.java:1040) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at tv.fakelove.stationtostation.MainActivity.onCreate(MainActivity.java:59) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.Activity.performCreate(Activity.java:5104) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
+0

你能提供完整的堆棧跟蹤嗎? – Seidr

+0

這是非常多的,我唯一的代碼是上面的和堆棧跟蹤顯示錯誤在線camera.take圖片 – MonkeyBonkey

+0

非常奇怪,應該有更多的信息可用比。您是否檢查過ADB logcat以獲取更多信息? – Seidr

回答

1

退房的answe r在前面的問題中提供:Android: "Camera.takePicture failed" Exception

顯然你需要在拍攝照片之前開始預覽,其中包括設置有效的預覽表面(我不認爲你在做什麼)。

此外,檢查步驟5-6在http://developer.android.com/reference/android/hardware/Camera.html

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
  4. If desired, call setDisplayOrientation(int).
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
  7. When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data.
  8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
  9. Call stopPreview() to stop updating the preview surface.
  10. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

一個想法奠定了 - 因爲你使用的是虛擬SurfaceView,你就需要設置SurfaceView的寬度/高度,以配合相機預覽尺寸的寬度/高度?

沒有完整的堆棧跟蹤,沒有更多的東西我可以看出你的代碼有問題。

+0

添加了logcat輸出...我添加了一個surfaceview到預覽並在拍攝pic之前調用startPreview ...對於虛擬表面視圖代碼,我從http://stackoverflow.com獲得了/ questions/10959767 /隱形表面視圖相機預覽 – MonkeyBonkey

+0

如果您以後仍然遇到問題,我會在下班後再放置一個副本應用程序,並查看是否可以使其工作。我的直覺是這與SurfaceView有關。你有沒有嘗試設置一個'真實'的Surface作爲預覽表面(即在主活動佈局中,並且有一個隱含的寬度/高度)? – Seidr

+0

作爲補充..確定相機正在成功打開嗎? – Seidr