我一直在尝试寻找类似的错误,但我遇到了困难……我还是 android 开发的新手,但希望有人能指出一些方向。
我有一个可以预览的相机应用程序,但是当我尝试单击按钮拍照时,我的应用程序崩溃了。有人能帮我吗?
[PhotoActivity.java]
public class PhotoActivity extends Activity {
public static final int MEDIA_TYPE_IMAGE = 1;
protected static final String TAG = "Activity";
private Camera mCamera;
private CameraPreview mCameraPreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
mCamera = getCameraInstant();
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
preview.addView(mCameraPreview);
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
Log.e("log", "mPicture"+mPicture);
mCamera.takePicture(null, null, mPicture);
}
}
);
}
PictureCallback mPicture = new PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if(pictureFile==null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e){
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
/**
* Helper method to access the camera returns null if
* it cannot get the camera or does not exist
* @return
*/
private Camera getCameraInstant(){
Camera camera = null;
try{
camera=Camera.open();
}catch (Exception e){
// cannot get camera or does not exist
}
return camera;
}
/** Create a File for saving the image */
private File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return mediaFile;
}
}
对不起所有的编码,但我真的需要一些帮助......在此先感谢。
更新
[CameraPreview.java]
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
//Constructor that obtains context and camera
public CameraPreview(Context context, Camera camera) {
super(context);
//this.mCamera = camera;
this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this); // we get notified when underlying surface is created and destroyed
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //this is a deprecated method, is not requierd after 3.0
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mCamera.release();
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
// left blank for now
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
if (mCamera != null) {
Log.d(TAG,"Stopping preview in SurfaceDestroyed().");
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
if(mSurfaceHolder.getSurface()==null){
//preview surface does not exist
return;
}
try {
mCamera.stopPreview();
}catch(Exception e){
//ignore: tried to stop a non-existent preview
}
// start preview with new settings
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (Exception e) {
// intentionally left blank for a test
Log.d(TAG, "Error starting camera preview: "+e.getMessage());
}
}
}
错误
05-09 21:19:29.013: E/AndroidRuntime(3823): FATAL EXCEPTION: main
05-09 21:19:29.013: E/AndroidRuntime(3823): java.lang.RuntimeException: Method called after release()
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.hardware.Camera.native_takePicture(Native Method)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.hardware.Camera.takePicture(Camera.java:746)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.hardware.Camera.takePicture(Camera.java:710)
05-09 20:21:01.214: E/AndroidRuntime(2813): at com.liu.photo.PhotoActivity$2.onClick(PhotoActivity.java:73)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.view.View.performClick(View.java:2486)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.view.View$PerformClick.run(View.java:9130)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.os.Handler.handleCallback(Handler.java:587)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.os.Looper.loop(Looper.java:130)
05-09 20:21:01.214: E/AndroidRuntime(2813): at android.app.ActivityThread.main(ActivityThread.java:3703)
05-09 20:21:01.214: E/AndroidRuntime(2813): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:21:01.214: E/AndroidRuntime(2813): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 20:21:01.214: E/AndroidRuntime(2813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-09 20:21:01.214: E/AndroidRuntime(2813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-09 20:21:01.214: E/AndroidRuntime(2813): at dalvik.system.NativeStart.main(Native Method)
这就是堆栈跟踪吗?我很新,还在学习这些术语。谢谢