Android takePicture failed
android, android-camera, camera, eclipse, java
Solution
Try adding the following permission, as on capturing it is not permitted to store the image...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Problem
I've been trying to make a Camera App for android but when I try to take the picture, it bugs and I have to force close. The LogCat is: ``` 07-14 12:41:25.195: D/dalvikvm(13549): GC_EXPLICIT freed 51K, 49% free 2694K/5187K, external 3474K/4422K, paused 26ms 07-14 12:41:25.195: D/AndroidRuntime(13549): Shutting down VM 07-14 12:41:25.195: W/dalvikvm(13549): threadid=1: thread exiting with uncaught exception (group=0x4001e560) 07-14 12:41:25.226: E/AndroidRuntime(13549): FATAL EXCEPTION: main 07-14 12:41:25.226: E/AndroidRuntime(13549): java.lang.RuntimeException: takePicture failed 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.hardware.Camera.native_takePicture(Native Method) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.hardware.Camera.takePicture(Camera.java:829) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.hardware.Camera.takePicture(Camera.java:793) 07-14 12:41:25.226: E/AndroidRuntime(13549): at com.mvitsor.camerax.cameraMainActivity.takepic(cameraMainActivity.java:53) 07-14 12:41:25.226: E/AndroidRuntime(13549): at com.mvitsor.camerax.cameraMainActivity$5.onClick(cameraMainActivity.java:44) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.view.View.performClick(View.java:2485) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.view.View$PerformClick.run(View.java:9089) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.os.Handler.handleCallback(Handler.java:587) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.os.Handler.dispatchMessage(Handler.java:92) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.os.Looper.loop(Looper.java:130) 07-14 12:41:25.226: E/AndroidRuntime(13549): at android.app.ActivityThread.main(ActivityThread.java:3859) 07-14 12:41:25.226: E/AndroidRuntime(13549): at java.lang.reflect.Method.invokeNative(Native Method) 07-14 12:41:25.226: E/AndroidRuntime(13549): at java.lang.reflect.Method.invoke(Method.java:507) 07-14 12:41:25.226: E/AndroidRuntime(13549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840) 07-14 12:41:25.226: E/AndroidRuntime(13549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598) 07-14 12:41:25.226: E/AndroidRuntime(13549): at dalvik.system.NativeStart.main(Native Method) ``` And here is the code: ``` package com.mvitsor.camerax; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.content.pm.ActivityInfo; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; public class cameraMainActivity extends Activity { /** Called when the activity is first created. */ Camera camera; SurfaceView preview; SurfaceHolder previewHolder; ImageButton takePic; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.main_camera_layout); preview = (SurfaceView) findViewById(R.id.SVpreview); previewHolder = preview.getHolder(); previewHolder.addCallback(surfaceCallback); previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); takePic = (ImageButton) findViewById(R.id.btn_take_pic); takePic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takepic(); } }); } public void takepic(){ camera.stopPreview(); System.gc(); camera.takePicture(null, null, photoCallback); //camera.takePicture(ShutterCallback, rawCallback, photoCallback); } private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) { Camera.Size result = null; for (Camera.Size size : parameters.getSupportedPreviewSizes()) { if (size.width <= width && size.height <= height) { if (result == null) { result = size; } else { int resultArea = result.width * result.height; int newArea = size.width * size.height; if (newArea > resultArea) { result = size; } } } } return (result); } SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); try { camera.setPreviewDisplay(previewHolder); } catch (Throwable t) { Toast toast = Toast.makeText(cameraMainActivity.this, "Exception in setPreviewDisplay", Toast.LENGTH_LONG); toast.show(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Camera.Parameters parameters = camera.getParameters(); Camera.Size bestSize = getBestPreviewSize(width, height, parameters); parameters.setPreviewSize(bestSize.width, bestSize.height); parameters.setPictureFormat(PixelFormat.JPEG); camera.setParameters(parameters); camera.startPreview(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; } }; /* Camera.ShutterCallback ShutterCallback = new Camera.ShutterCallback() { public void onShutter() { } }; PictureCallback rawCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { } }; */ Camera.PictureCallback photoCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { // Save the image JPEG data to the SD card FileOutputStream outStream = null; try { outStream = new FileOutputStream("/sdcard/test.jpg"); outStream.write(data); outStream.close(); } catch (FileNotFoundException e) { Log.d("CAMERA", e.getMessage()); } catch (IOException e) { Log.d("CAMERA", e.getMessage()); } camera.startPreview(); /*new SavePhotoTask().execute(data); camera.startPreview();*/ } }; } ``` I've tried both `camera.takePicture(null, null, photoCallback);` and `camera.takePicture(ShutterCallback, rawCallback, photoCallback);`, as you can see, and I also read somewhere that `System.gc();` could work, but it didn't. Also, I read that a common problem was forgetting to restart the preview after taking the picture, but I'm calling `camera.startPreview();` after it, so I really don't know what's going on! I'm running Android 2.3.3 (Motorola Razr). (it looks like many people are having problems with camera on Android 2.3.3...). I'd be really grateful if someone could help! Vitor Martes Sternlicht I'VE UPLOADED THE WHOLE PROJECT TO http://www.4shared.com/rar/meSWrQPm/CameraTest.html I downloaded an app that showed this Log... Maybe there's something here that could help: it looks like the problem occured when creating the files directory. How can I solve this? ``` 07-15 12:43:36.578 27203 27487 W ActivityManager: Force finishing activity com.mvitsor.camerax/.cameraMainActivity 07-15 12:43:36.601 27203 27221 W ApplicationContext: Unable to create files directory ``` Here is the complete Log ``` 07-15 13:09:08.226 27203 27582 W ActivityManager: Force finishing activity com.mvitsor.camerax/.cameraMainActivity 07-15 12:43:36.601 27203 27221 W ApplicationContext: Unable to create files directory 07-15 13:09:08.414 27203 27221 I ActivityManager: Config changed: { scale=1.0 imsi=724/10 loc=pt_BR touch=3 keys=1/1/2 nav=1/1 orien=1 layout=268435490 uiMode=17 seq=139} 07-15 13:09:08.726 27203 27219 W ActivityManager: Activity pause timeout for HistoryRecord{40562350 com.mvitsor.camerax/.cameraMainActivity} 07-15 13:09:09.132 27203 27219 I ActivityManager: No longer want android.process.media (pid 11091): hidden #21 07-15 13:09:09.515 27203 27488 I WindowManager: WIN DEATH: Window{40d859a8 com.mvitsor.camerax/com.mvitsor.camerax.cameraMainActivity paused=false} 07-15 13:09:09.515 27203 27499 I ActivityManager: Process com.mvitsor.camerax (pid 11848) has died. 07-15 13:09:09.515 27203 27426 I WindowManager: WIN DEATH: Window{40ef8c70 SurfaceView paused=false} 07-15 13:09:09.539 27203 27210 I MotOverlay: disable_streaming_locked 07-15 13:09:09.539 27203 27210 I MotOverlay: Destroying overlay/fd=300/obj=4650c000 07-15 13:09:09.539 27498 24620 D IMirrorIpc: onTransact (2) 07-15 13:09:09.539 27498 24620 D MirrorIpc: unregisterCallback: client 0 07-15 13:09:09.539 27498 24620 D MirrorUCMgrIpcCB: ipcInit - Client already exists 07-15 13:09:09.539 27498 24620 D MirrorEngine: hdmiMirrorActiveOverlay/0 07-15 13:09:09.539 27174 11890 D OverlayDisplayAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OverlayDisplayAdapter.cpp:714 processHalMsg - Display thread received DISPLAY_EXIT command from Camera HAL. 07-15 13:09:09.539 27174 11890 D OverlayDisplayAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OverlayDisplayAdapter.cpp:715 processHalMsg - Stopping display thread... 07-15 13:09:09.539 27174 11890 D OverlayDisplayAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OverlayDisplayAdapter.cpp:735 processHalMsg - +Signalling display semaphore 07-15 13:09:09.539 27174 11890 D OverlayDisplayAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OverlayDisplayAdapter.cpp:740 processHalMsg - -Signalling display semaphore 07-15 13:09:09.539 27174 27672 E CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:1517 stop - AppCallbackNotifier already in stopped state 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:161 notificationThread - Notification Thread received message from Camera HAL 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:848 processMessage - +Msg get... 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:850 processMessage - -Msg get... 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:869 processMessage - Received NOTIFIER_EXIT command from Camera HAL 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:880 processMessage - +Signalling semaphore from CameraHAL.. 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:883 processMessage - -Signalling semaphore from CameraHAL.. 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:165 notificationThread - Notification Thread exiting. 07-15 13:09:09.539 27174 11876 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:190 notificationThread - Notification Thread exited. 07-15 13:09:09.539 27174 27672 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:938 ~AppCallbackNotifier - Stopping Event Provider 07-15 13:09:09.539 27174 27672 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/AppCallbackNotifier.cpp:946 ~AppCallbackNotifier - Stopping Frame Provider 07-15 13:09:09.539 27174 27672 D CameraHal: hardware/ti/omap4/omap3/camera-omap4/src/BaseCameraAdapter.cpp:476 sendCommand - Set time out 07-15 13:09:09.539 27174 27672 D OMXCameraAdapter: 07-15 13:09:09.539 27174 27672 D OMXCameraAdapter: 07-15 13:09:09.539 27174 27672 D OMXCameraAdapter: **** ~OMXCameraAdapter called() ! **** 07-15 13:09:09.539 27174 27672 D OMXCameraAdapter: 07-15 13:09:09.585 27174 27672 E DOMX_RPC: Closing IPC 07-15 13:09:09.585 27174 11875 D OMXCameraAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OMXCameraAdapter/OMXCameraAdapter.cpp:9462 Handler - msg.command = -1 07-15 13:09:09.585 27174 11875 E OMXCameraAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OMXCameraAdapter/OMXCameraAdapter.cpp:9476 Handler - Exiting command handler 07-15 13:09:09.585 27174 27672 D OMXCameraAdapter: 07-15 13:09:09.585 27174 27672 D OMXCameraAdapter: 07-15 13:09:09.585 27174 27672 D OMXCameraAdapter: **** ~OMXCameraAdapter exiting ! **** 07-15 13:09:09.585 27174 27672 D OMXCameraAdapter: 07-15 13:09:09.601 27174 27672 D CameraHal: 07-15 13:09:09.601 27174 27672 D CameraHal: 07-15 13:09:09.601 27174 27672 D CameraHal: **** ~CameraHal() **** 07-15 13:09:09.601 27174 27672 D CameraHal: 07-15 13:09:09.601 27174 27672 D CameraHal: 07-15 13:09:11.523 27203 27552 I ActivityManager: Starting: Intent { act=android.intent.action.MAIN flg=0x10500000 cmp=de.softxperience.android.nedebug/.LogcatActivity } from pid 27542 ```