我正在制作一个可以显示相机预览的Android活动。我试图直接拍摄那张照片,而没有调用其他相机应用程序(换句话说,我不想使用意图)。
单击“停止预览”按钮时,应用程序崩溃。我添加了使用停止预览捕获照片的代码。以下是我的源代码:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
@SuppressWarnings("deprecation")
public class CameraOpened extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
static final int REQUEST_IMAGE_CAPTURE = 1;
int count;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
private final String tag = "VideoServer";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_opened);
Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
if(!previewing){
camera = Camera.open();
camera.setDisplayOrientation(90);
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}});
buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(camera != null && previewing){
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
captureImage();
rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Log", "onPictureTaken - raw");
}
};
shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.i("Log", "onShutter'd");
}
};
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/DCIM/jgjk.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d("Log", "onPictureTaken - jpeg");
}
};
}
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera_opened, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
private void captureImage() {
// TODO Auto-generated method stub
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
我的应用程序的logcat如下:
10-21 15:51:06.343: V/ContextImpl(9564): ----- packageName = com.camera is NOT LOCKED -----
10-21 15:51:07.743: D/Camera(9564): [CTA] Camera open urrentpackagename = com.camera
10-21 15:51:07.753: D/Camera(9564): [CTA] Camera open application != null
10-21 15:51:07.753: D/Camera(9564): [CTA] check CTA permisson mAllowUsing = false
10-21 15:51:07.753: D/Camera(9564): [CTA] Camera open urrentpackagename = com.camera
10-21 15:51:07.753: D/Camera(9564): [CTA] Camera open application != null
10-21 15:51:07.753: D/Camera(9564): [CTA] after check permission mAllowUsing = com.camera
10-21 15:51:08.803: I/Choreographer(9564): Skipped 63 frames! The application may be doing too much work on its main thread.
10-21 15:51:11.453: D/AndroidRuntime(9564): Shutting down VM
10-21 15:51:11.453: W/dalvikvm(9564): threadid=1: thread exiting with uncaught exception (group=0x430f9140)
10-21 15:51:11.453: E/AndroidRuntime(9564): FATAL EXCEPTION: main
10-21 15:51:11.453: E/AndroidRuntime(9564): Process: com.camera, PID: 9564
10-21 15:51:11.453: E/AndroidRuntime(9564): java.lang.NullPointerException
10-21 15:51:11.453: E/AndroidRuntime(9564): at com.camera.CameraOpened.captureImage(CameraOpened.java:155)
10-21 15:51:11.453: E/AndroidRuntime(9564): at com.camera.CameraOpened.access$0(CameraOpened.java:153)
10-21 15:51:11.453: E/AndroidRuntime(9564): at com.camera.CameraOpened$2.onClick(CameraOpened.java:79)
10-21 15:51:11.453: E/AndroidRuntime(9564): at android.view.View.performClick(View.java:4487)
10-21 15:51:11.453: E/AndroidRuntime(9564): at android.view.View$PerformClick.run(View.java:18746)
10-21 15:51:11.453: E/AndroidRuntime(9564): at android.os.Handler.handleCallback(Handler.java:733)
10-21 15:51:11.453: E/AndroidRuntime(9564): at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 15:51:11.453: E/AndroidRuntime(9564): at android.os.Looper.loop(Looper.java:149)
10-21 15:51:11.453: E/AndroidRuntime(9564): at android.app.ActivityThread.main(ActivityThread.java:5268)
10-21 15:51:11.453: E/AndroidRuntime(9564): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 15:51:11.453: E/AndroidRuntime(9564): at java.lang.reflect.Method.invoke(Method.java:515)
10-21 15:51:11.453: E/AndroidRuntime(9564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-21 15:51:11.453: E/AndroidRuntime(9564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
10-21 15:51:11.453: E/AndroidRuntime(9564): at dalvik.system.NativeStart.main(Native Method)
10-21 15:51:14.223: I/Process(9564): Sending signal. PID: 9564 SIG: 9
10-21 15:51:14.493: I/dalvikvm(9811): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
10-21 15:51:14.493: W/dalvikvm(9811): VFY: unable to resolve virtual method 11390: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
10-21 15:51:14.493: D/dalvikvm(9811): VFY: replacing opcode 0x6f at 0x0000
10-21 15:51:14.493: I/dalvikvm(9811): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
10-21 15:51:14.493: W/dalvikvm(9811): VFY: unable to resolve virtual method 11396: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
10-21 15:51:14.493: D/dalvikvm(9811): VFY: replacing opcode 0x6f at 0x0000
10-21 15:51:14.503: I/dalvikvm(9811): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
10-21 15:51:14.503: W/dalvikvm(9811): VFY: unable to resolve virtual method 8966: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
10-21 15:51:14.503: D/dalvikvm(9811): VFY: replacing opcode 0x6e at 0x000e
10-21 15:51:14.503: I/dalvikvm(9811): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
10-21 15:51:14.503: W/dalvikvm(9811): VFY: unable to resolve virtual method 366: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
10-21 15:51:14.503: D/dalvikvm(9811): VFY: replacing opcode 0x6e at 0x0002
10-21 15:51:14.503: I/dalvikvm(9811): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-21 15:51:14.503: W/dalvikvm(9811): VFY: unable to resolve virtual method 388: Landroid/content/res/TypedArray;.getType (I)I
10-21 15:51:14.503: D/dalvikvm(9811): VFY: replacing opcode 0x6e at 0x0002
10-21 15:51:14.563: E/IMGSRV(9811): :0: PVRDRMOpen: TP3, ret = 47
10-21 15:51:14.563: E/IMGSRV(9811): :0: PVRDRMOpen: TP3, ret = 50
10-21 15:51:14.563: E/IMGSRV(9811): :0: PVRDRMOpen: TP3, ret = 51
10-21 15:51:14.563: E/IMGSRV(9811): :0: PVRDRMOpen: TP3, ret = 51
10-21 15:51:14.563: E/IMGSRV(9811): :0: PVRDRMOpen: TP3, ret = 51
10-21 15:51:14.573: E/IMGSRV(9811): :0: PVRDRMOpen: TP3, ret = 53
10-21 15:51:14.593: D/OpenGLRenderer(9811): Enabling debug mode 0
为什么应用程序崩溃而无法捕获照片?
答案 0 :(得分:1)
您可能会将null作为参数发送到:
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
(第155行)
原因是您在onClickListner中初始化了shutterCallback,rawCallback和jpegCallback,尝试在'onCreate'方法中执行此操作,但在onClickListner块之外。