Camera service crashes in Android-Collection of common programming errors
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
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 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) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < 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) < 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 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();
}
}
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:
What am I missing here?
Edit: Adding logcat output
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@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@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@[email protected]@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): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit