exception in camera application in android
I am getting an exception using a custom front camera. On button click I am calling camera.takepicture
. Can anybody tell me what the problem is and how to fix it?
09-23 11:24:35.062: ERROR/AndroidRuntime(949): FATAL EXCEPTION: main
09-23 11:24:35.062: ERROR/AndroidRuntime(949): java.lang.RuntimeException: Method called after release()
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.hardware.Camera.native_takePicture(Native Method)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.hardware.Camera.takePicture(Camera.java:746)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at com.camera.test.CameraActivity.onClick(CameraActivity.java:42)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.view.View.performClick(View.java:2485)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.view.View$PerformClick.run(View.java:9080)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.os.Handler.handleCallback(Handler.java:587)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.os.Looper.loop(Looper.java:123)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at java.lang.reflect.Method.invoke(Method.java:507)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-23 11:24:35.062: ERROR/AndroidRuntime(949): at dalvik.system.NativeStart.main(Native Method)
09-23 11:24:35.082: WARN/ActivityManager(107): Force finishing activity com.camera.test/.CameraActivity
my xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:configChanges="orientation">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Button" android:id="@+id/button1">
</Button>
<com.camera.test.Preview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/preview" android:layout_marginTop="100px"/>
</LinearLayout>
</FrameLayout>
my code
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CameraActivity extends Activity implements OnClickListener {
Preview p=null;
private Button button1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
p=(Preview)findViewById(R.id.preview);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
//p.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
p.mCamera.takePicture(shutterCallback, null, jpegCallback);
/* p.mCamera.(new Camera.AutoFocusCallback()
{
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback()
{
public void onShutter()
{ // Play your sound here.
}
};
public void onAutoFocus(boolean success, Camera camera)
{
p.mCamera.takePicture(shutterCallback, null, jpegCallback);
}
});*/
}
/*p.mCamera.startPreview();
p.mCamera.takePicture(shutterCallback, null, jpegCallback);
*/
// TODO Auto-generated method stub
/*p.mCamera.autoFocus(new Camera.AutoFocusCallback()
{
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback()
{ public void onShutter()
{
// Play your sound here.
}
};
public void onAutoFocus(boolean success, Camera camera)
{
}
}); */
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() { // <6>
public void onShutter() {
//Log.d(TAG, "onShutter'd");
}
};
//Handles data for raw picture
Camera.PictureCallback rawCallback = new Camera.PictureCallback() { // <7>
public void onPictureTaken(byte[] data, Camera camera) {
// Log.d(TAG, "onPictureTaken - raw");
}
};
// Handles data for jpeg picture
Camera.PictureCallback jpegCallback = new Camera.PictureCallback() { // <8>
public void onPictureTaken(byte[] data, Camera camera) {
//YuvImage image = new YuvImage(data, parameters.getPreviewFormat(), size.width, size.height, null);
//decodeYUV(argb8888, data, camSize.width, camSize.height);
//Bitmap bitmap = Bitmap.createBitmap(argb8888, camSize.width,
//camSize.height, Config.ARGB_8888);
// p.mCamera.startPreview();
Toast.makeText(CameraActivity.this, "Writing a file", Toast.LENGTH_LONG).show();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath()+"/Vijay");
dir.mkdir();
Toast.makeText(CameraActivity.this, dir.getAbsolutePath(), Toast.LENGTH_LONG).show();
//dir.mkdirs();
//Toast.makeText(CameraActivity.this,"%d.jpg"System.currentTimeMillis()+"", Toast.LENGTH_LONG).show();
File out = new File(dir,String.format("%d.jpg", System.currentTimeMillis()));
try {
out.createNewFile();
Toast.makeText(CameraActivity.this, "created new file", Toast.LENGTH_LONG).show();
Toast.makeText(CameraActivity.this, out.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DataOutputStream fo = null;
try {
fo = new DataOutputStream( new FileOutputStream(out));
//write what you want to fo
fo.write(data);
fo.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(CameraActivity.this, "entering exception",Toast.LENGTH_LONG).show();
PrintWriter pw;
try
{
File sdCard1 = Environment.getExternalStorageDirectory();
File dir1 = new File(sdCard1.getAbsolutePath()+"/Log");
dir1.mkdir();
File out1 = new File(dir1,String.format("%d.txt", System.currentTimeMillis()));
try {
out1.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pw = new PrintWriter(out1);
e.printStackTrace(pw);
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
//
}
// TODO: handle exception
//e.printStackTrace();
}
Toast.makeText(getBaseContext(), "Preview", Toast.LENGTH_SHORT).show();
//camera.release();
}
};
}
this is my preview
package com.camera.test;
import java.io.IOException;
import java.util.NoSuchElementException;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class Preview extends SurfaceView implements SurfaceHolder.Callback {
Camera mCamera;
private final SurfaceHolder mHolder;
final static int SUPPORTED_WIDTH = 640;
final static int SUPPORTED_HEIGHT = 480;
public Preview(Context context, AttributeSet attributes) {
super(context, attributes);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
/**
* Works in API level >= 10.
*
* @return Front camera handle.
*/
Camera getFrontFacingCamera() throws NoSuchElementException
{
final Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int cameraIndex = 0; cameraIndex < Camera.getNumberOfCameras(); cameraIndex++)
{
Camera.getCameraInfo(cameraIndex, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
try {
return Camera.open(cameraIndex);
} catch (final RuntimeException e)
{
e.printStackTrace();
}
}
}
throw new NoSuchElementException("Can't find front camera.");
}
/**
* Works in API level >= 7 at Samsung Galaxy S.
*
* @param Camera handle.
*/
void setFrontCamera(Camera camera) {
final Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id", 2);
try {
camera.setParameters(parameters);
} catch (final RuntimeException e) {
// If we can't set front camera it means that device hasn't got "camera-id". Maybe it's not Galaxy S.
e.printStackTrace();
}
}
/**
* @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)
*/
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Now that the size is known, set up the camera parameters and begin the preview.
final Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(SUPPORTED_WIDTH, SUPPORTED_HEIGHT);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
/**
* The Surface has been created, acquire the camera and tell it where to draw.
*
* @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder)
*/
public void surfaceCreated(SurfaceHolder holder) {
if (android.os.Build.VERSION.SDK_INT >= 10) {
mCamera = getFrontFacingCamera();
mCamera.setDisplayOrientation(90);
} else {
mCamera = Camera.open();
setFrontCamera(mCamera);
}
try {
mCamera.setPreviewDisplay(holder);
} catch (final IOException e) {
//mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
e.printStackTrace();
}
}
/**
* @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)
*/
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevi开发者_JAVA技巧ce object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
//mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
Thanks
Uncomment the line mCamera.setPreviewCallback(null);
in public void surfaceDestroyed(SurfaceHolder holder)
callback.
精彩评论