Camera service crashes in Android
<p>I am writing and Android app that uses the camera. Once activity saves and image and another activity processes it. Here is the code for the activity that saves an image</p>

<pre><code>import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class TakePictureActivity extends Activity implements
        SurfaceHolder.Callback {
    private Camera mCamera;
    private Button takePictureButton;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private boolean mPreviewRunning;
    private String currentPictureName;
    private String sdcardPath = "/sdcard/DCIM";

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera c) {
            currentPictureName = Long.toString(System.currentTimeMillis())
                    + ".jpg";

            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(sdcardPath
                        + currentPictureName);
                outStream.write(data);
                outStream.close();

                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                mCamera.startPreview();
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.takepicture);

        mSurfaceView = (SurfaceView) findViewById(R.id.preview);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        takePictureButton = (Button) findViewById(R.id.buttonTakePhoto);
        takePictureButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mCamera.takePicture(null, null, mPictureCallback);
            }
        });
    }

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
    }

    private Size getOptimalPreviewSize(List&lt;Size&gt; sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }
        Camera.Parameters p = mCamera.getParameters();
        List&lt;Camera.Size&gt; previewSizes = p.getSupportedPreviewSizes();
        Size s = getOptimalPreviewSize(previewSizes, w, h);

        p.setPreviewSize(s.width, s.height);
        // p.setPreviewSize(s.height, s.width);
        mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

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

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        }

        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            mCamera.takePicture(null, null, mPictureCallback);
            return true;
        }
        return false;
    }

    public void snapClicked(View view) {
        Log.e("@@@@@@@@@", "snapclicked called");
        mCamera.takePicture(null, null, mPictureCallback);
        Toast.makeText(this, "Picture saved on sd card", Toast.LENGTH_SHORT)
                .show();
    }
}
</code></pre>

<p>The problem is, when I use this app, the camera service crashes after two or three pics and my app crashes with it. It does save the few pictures. And after it crashes, the built in camera app does not open. Here is the manifest:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.apps.objectdetection"
    android:versionCode="1"
    android:versionName="1.0" &gt;

    &lt;uses-sdk android:minSdkVersion="10" /&gt;

    &lt;application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" &gt;
        &lt;activity
            android:label="@string/app_name"
            android:name=".ObjectDetection" &gt;
            &lt;intent-filter &gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        &lt;activity android:name=".TakePictureActivity" /&gt;
        &lt;activity android:name=".AnalyzeActivity" /&gt;
    &lt;/application&gt;

    &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;

    &lt;uses-feature android:name="android.hardware.camera" /&gt;
    &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;

&lt;/manifest&gt;
</code></pre>

<p>What am I missing here?</p>

<p><strong>Edit:</strong>
Adding logcat output</p>

<pre><code>    I/PackageManager(   69): Removing non-system package:com.apps.objectdetection
I/ActivityManager(   69): Force stopping package com.apps.objectdetection uid=10036
D/PackageManager(   69): Scanning package com.apps.objectdetection
I/PackageManager(   69): Package com.apps.objectdetection codePath changed from /data/app/com.apps.objectdetection-1.apk to /data/app/com.apps.objectdetection-2.apk; Retaining data and using new
I/PackageManager(   69): Unpacking native libraries for /data/app/com.apps.objectdetection-2.apk
D/dalvikvm(   69): GC_CONCURRENT freed 1266K, 48% free 4269K/8199K, external 4373K/5573K, paused 9ms+7ms
D/installd(   35): DexInv: --- BEGIN '/data/app/com.apps.objectdetection-2.apk' ---
D/dalvikvm(  333): DexOpt: load 64ms, verify+opt 376ms
D/installd(   35): DexInv: --- END '/data/app/com.apps.objectdetection-2.apk' (success) ---
W/PackageManager(   69): Code path for pkg : com.apps.objectdetection changing from /data/app/com.apps.objectdetection-1.apk to /data/app/com.apps.objectdetection-2.apk
W/PackageManager(   69): Resource path for pkg : com.apps.objectdetection changing from /data/app/com.apps.objectdetection-1.apk to /data/app/com.apps.objectdetection-2.apk
D/PackageManager(   69):   Activities: com.apps.objectdetection.ObjectDetection com.apps.objectdetection.TakePictureActivity com.apps.objectdetection.AnalyzeActivity
I/ActivityManager(   69): Force stopping package com.apps.objectdetection uid=10036
I/installd(   35): move /data/dalvik-cache/data@app@com.apps.objectdetection-2.apk@classes.dex -&gt; /data/dalvik-cache/data@app@com.apps.objectdetection-2.apk@classes.dex
D/PackageManager(   69): New package installed in /data/app/com.apps.objectdetection-2.apk
I/ActivityManager(   69): Force stopping package com.apps.objectdetection uid=10036
D/dalvikvm(  139): GC_EXPLICIT freed 75K, 52% free 2908K/5959K, external 4984K/5293K, paused 65ms
D/dalvikvm(  194): GC_EXPLICIT freed 292K, 52% free 2769K/5703K, external 1625K/2137K, paused 158ms
I/ActivityManager(   69): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=334 uid=10009 gids={}
W/RecognitionManagerService(   69): no available voice recognition services found
I/ActivityThread(  334): Pub com.svox.pico.providers.SettingsProvider: com.svox.pico.providers.SettingsProvider
D/dalvikvm(   69): GC_EXPLICIT freed 445K, 49% free 4242K/8199K, external 4373K/5573K, paused 80ms
I/installd(   35): unlink /data/dalvik-cache/data@app@com.apps.objectdetection-1.apk@classes.dex
D/AndroidRuntime(  240): Shutting down VM
D/dalvikvm(  240): GC_CONCURRENT freed 100K, 72% free 293K/1024K, external 0K/0K, paused 3ms+1ms
D/jdwp    (  240): adbd disconnected
I/AndroidRuntime(  240): NOTE: attach of thread 'Binder Thread #3' failed
D/AndroidRuntime(  347): 
D/AndroidRuntime(  347): &gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START com.android.internal.os.RuntimeInit &lt;&lt;&lt;&lt;&lt;&lt;
D/AndroidRuntime(  347): CheckJNI is ON
D/AndroidRuntime(  347): Calling main entry com.android.commands.am.Am
I/ActivityManager(   69): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.apps.objectdetection/.ObjectDetection } from pid 347
I/ActivityManager(   69): Start proc com.apps.objectdetection for activity com.apps.objectdetection/.ObjectDetection: pid=355 uid=10036 gids={1006, 1015}
D/AndroidRuntime(  347): Shutting down VM
D/dalvikvm(  347): GC_CONCURRENT freed 101K, 69% free 318K/1024K, external 0K/0K, paused 2ms+1ms
D/jdwp    (  347): adbd disconnected
I/AndroidRuntime(  347): NOTE: attach of thread 'Binder Thread #3' failed
E/TAG     (  355): OD onCraete called!
D/CameraHardwareStub(   34): initHeapLocked: preview size=320x240
I/StagefrightPlayer(   34): setDataSource('/system/media/audio/ui/camera_click.ogg')
I/StagefrightPlayer(   34): setDataSource('/system/media/audio/ui/VideoRecord.ogg')
D/CameraHardwareStub(   34): initHeapLocked: preview size=320x240
I/ActivityManager(   69): Displayed com.apps.objectdetection/.ObjectDetection: +2s500ms (total +46s538ms)
I/ActivityManager(   69): Displayed com.android.launcher/com.android.launcher2.Launcher: +46s542ms
I/InputReader(   69): Device reconfigured: id=0x0, name=qwerty2, display size is now 480x800
I/InputManager-Callbacks(   69): No virtual keys found for device qwerty2.
I/ARMAssembler(   69): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x444ea520:0x444ea6d8] in 2041001 ns
I/ARMAssembler(   69): generated scanline__00000077:03515104_00001004_00000000 [ 65 ipp] (85 ins) at [0x444ea6e0:0x444ea834] in 701000 ns
I/ARMAssembler(   69): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x444ea838:0x444eaa00] in 642000 ns
E/SurfaceFlinger(   69): texture=11, using format 17, which is not supported by the GL
D/AudioSink(   34): bufferCount (4) is too small and increased to 12
E/SurfaceFlinger(   69): texture=7, using format 17, which is not supported by the GL
D/MediaScannerService(  226): start scanning volume external
V/MediaScanner(  226): pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@40528098
V/MediaScanner(  226): /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@40528098
D/MediaScanner(  226):  prescan time: 81ms
D/MediaScanner(  226):     scan time: 53ms
D/MediaScanner(  226): postscan time: 26ms
D/MediaScanner(  226):    total time: 160ms
D/MediaScannerService(  226): done scanning volume external
E/SurfaceFlinger(   69): texture=7, using format 17, which is not supported by the GL
D/MediaScannerService(  226): start scanning volume external
D/MediaScanner(  226):  prescan time: 88ms
D/MediaScanner(  226):     scan time: 55ms
D/MediaScanner(  226): postscan time: 0ms
D/MediaScanner(  226):    total time: 143ms
D/MediaScannerService(  226): done scanning volume external
I/InputDispatcher(   69): Application is not responding: Window{4074e398 com.apps.objectdetection/com.apps.objectdetection.ObjectDetection paused=false}.  5006.2ms since event, 5005.8ms since wait started
I/WindowManager(   69): Input event dispatching timed out sending to com.apps.objectdetection/com.apps.objectdetection.ObjectDetection
I/Process (   69): Sending signal. PID: 355 SIG: 3
I/dalvikvm(  355): threadid=4: reacting to signal 3
I/dalvikvm(  355): Wrote stack traces to '/data/anr/traces.txt'
I/Process (   69): Sending signal. PID: 69 SIG: 3
I/dalvikvm(   69): threadid=4: reacting to signal 3
I/dalvikvm(   69): Wrote stack traces to '/data/anr/traces.txt'
I/Process (   69): Sending signal. PID: 131 SIG: 3
I/dalvikvm(  131): threadid=4: reacting to signal 3
I/dalvikvm(  131): Wrote stack traces to '/data/anr/traces.txt'
I/Process (   69): Sending signal. PID: 135 SIG: 3
I/dalvikvm(  135): threadid=4: reacting to signal 3
I/dalvikvm(  135): Wrote stack traces to '/data/anr/traces.txt'
D/dalvikvm(   69): GC_EXPLICIT freed 479K, 48% free 4310K/8199K, external 4373K/5573K, paused 130ms
I/Process (   69): Sending signal. PID: 226 SIG: 3
I/dalvikvm(  226): threadid=4: reacting to signal 3
I/dalvikvm(  226): Wrote stack traces to '/data/anr/traces.txt'
E/ActivityManager(   69): ANR in com.apps.objectdetection (com.apps.objectdetection/.ObjectDetection)
E/ActivityManager(   69): Reason: keyDispatchingTimedOut
E/ActivityManager(   69): Load: 2.06 / 1.16 / 0.44
E/ActivityManager(   69): CPU usage from 28574ms to 0ms ago:
E/ActivityManager(   69):   0.2% 41/adbd: 0% user + 0.2% kernel / faults: 64 minor
E/ActivityManager(   69):   0.1% 69/system_server: 0.1% user + 0% kernel / faults: 2 minor
E/ActivityManager(   69):   0% 131/com.android.phone: 0% user + 0% kernel / faults: 7 minor
E/ActivityManager(   69): 1.2% TOTAL: 0.4% user + 0.8% kernel + 0% softirq
E/ActivityManager(   69): CPU usage from 985ms to 1562ms later:
E/ActivityManager(   69):   14% 69/system_server: 7.1% user + 7.1% kernel / faults: 5 minor
E/ActivityManager(   69):     8.9% 103/InputDispatcher: 5.3% user + 3.5% kernel
E/ActivityManager(   69):     5.3% 70/HeapWorker: 1.7% user + 3.5% kernel
E/ActivityManager(   69):   1% 226/android.process.media: 1% user + 0% kernel
E/ActivityManager(   69):     1% 233/Binder Thread #: 1% user + 0% kernel
E/ActivityManager(   69): 17% TOTAL: 7% user + 10% kernel
I/InputDispatcher(   69): Dropping event because the pointer is not down.
D/SntpClient(   69): request time failed: java.net.SocketException: Address family not supported by protocol
I/dalvikvm(   69): Jit: resizing JitTable from 1024 to 2048
D/SntpClient(   69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient(   69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient(   69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient(   69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient(   69): request time failed: java.net.SocketException: Address family not supported by protocol
</code></pre>

<p>And here is the contents of <code>traces.txt</code></p>

<pre><code>----- pid 355 at 2012-02-05 23:53:50 -----
Cmd line: com.apps.objectdetection

DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0x4001f1a8 self=0xce48
  | sysTid=355 nice=0 sched=0/0 cgrp=default handle=-1345006528
  | schedstat=( 726128083 1938849180 195 )
  at android.hardware.Camera.native_takePicture(Native Method)
  at android.hardware.Camera.takePicture(Camera.java:746)
  at android.hardware.Camera.takePicture(Camera.java:710)
  at com.apps.objectdetection.TakePictureActivity$2.onClick(TakePictureActivity.java:76)
  at android.view.View.performClick(View.java:2485)
  at android.view.View$PerformClick.run(View.java:9080)
  at android.os.Handler.handleCallback(Handler.java:587)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:3683)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:507)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  at dalvik.system.NativeStart.main(Native Method)

"Binder Thread #2" prio=5 tid=8 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0x40512758 self=0x1f8c40
  | sysTid=367 nice=0 sched=0/0 cgrp=default handle=2557464
  | schedstat=( 12951001 32247003 12 )
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=7 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0x405111b0 self=0x117770
  | sysTid=366 nice=0 sched=0/0 cgrp=default handle=1144624
  | schedstat=( 18285003 36422003 16 )
  at dalvik.system.NativeStart.run(Native Method)

"Compiler" daemon prio=5 tid=6 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0x4050eca8 self=0x116c00
  | sysTid=365 nice=0 sched=0/0 cgrp=default handle=1143144
  | schedstat=( 2489001 48433004 6 )
  at dalvik.system.NativeStart.run(Native Method)

"JDWP" daemon prio=5 tid=5 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0x4050ebf8 self=0x116ac8
  | sysTid=364 nice=0 sched=0/0 cgrp=default handle=580152
  | schedstat=( 22429003 84657008 23 )
  at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
  | group="system" sCount=0 dsCount=0 obj=0x4050eb38 self=0x116fa8
  | sysTid=363 nice=0 sched=0/0 cgrp=default handle=576032
  | schedstat=( 11413002 30484003 8 )
  at dalvik.system.NativeStart.run(Native Method)

"GC" daemon prio=5 tid=3 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0x4050ea90 self=0x8d848
  | sysTid=358 nice=0 sched=0/0 cgrp=default handle=576384
  | schedstat=( 723000 9093001 2 )
  at dalvik.system.NativeStart.run(Native Method)

"HeapWorker" daemon prio=5 tid=2 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0x4050e9d8 self=0x1165f0
  | sysTid=357 nice=0 sched=0/0 cgrp=default handle=969568
  | schedstat=( 77777007 532447054 23 )
  at dalvik.system.NativeStart.run(Native Method)

----- end 355 -----
</code></pre>
