我总是一个NullPointerException当我尝试使用此代码拍照。 它总是失败的camera.takePicture。
我试图谷歌这个问题,并没有发现任何东西。 任何帮助,将不胜感激。
下面是代码
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class CameraService extends Activity {
final static String DEBUG_TAG = "MakePhotoActivity";
private Camera camera;
private int cameraId = 0;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// do we have a camera?
if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
.show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
}
public void onClick(View view) {
camera.takePicture(null, null,
new PhotoHandler(getApplicationContext()));
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d(DEBUG_TAG, "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
@Override
protected void onPause() {
if (camera != null) {
camera.release();
camera = null;
}
super.onPause();
}
}
代码PhotoHandler
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
public class PhotoHandler implements PictureCallback{
private final Context context;
public PhotoHandler(Context context) {
this.context = context;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d(CameraService.DEBUG_TAG, "Can't create directory to save image.");
Toast.makeText(context, "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(context, "New Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
} catch (Exception error) {
Log.d(CameraService.DEBUG_TAG, "File" + filename + "not saved: "
+ error.getMessage());
Toast.makeText(context, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
}
private File getDir() {
File sdDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "CameraAPIDemo");
}
}
该logcat的输出
04-15 18:24:42.316: E/AndroidRuntime(23331): FATAL EXCEPTION: main
04-15 18:24:42.316: E/AndroidRuntime(23331): java.lang.IllegalStateException: Could not execute method of the activity
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.view.View$1.onClick(View.java:3599)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.view.View.performClick(View.java:4204)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.view.View$PerformClick.run(View.java:17355)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.os.Handler.handleCallback(Handler.java:725)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.os.Looper.loop(Looper.java:137)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-15 18:24:42.316: E/AndroidRuntime(23331): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:24:42.316: E/AndroidRuntime(23331): at java.lang.reflect.Method.invoke(Method.java:511)
04-15 18:24:42.316: E/AndroidRuntime(23331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-15 18:24:42.316: E/AndroidRuntime(23331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-15 18:24:42.316: E/AndroidRuntime(23331): at dalvik.system.NativeStart.main(Native Method)
04-15 18:24:42.316: E/AndroidRuntime(23331): Caused by: java.lang.reflect.InvocationTargetException
04-15 18:24:42.316: E/AndroidRuntime(23331): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:24:42.316: E/AndroidRuntime(23331): at java.lang.reflect.Method.invoke(Method.java:511)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.view.View$1.onClick(View.java:3594)
04-15 18:24:42.316: E/AndroidRuntime(23331): ... 11 more
04-15 18:24:42.316: E/AndroidRuntime(23331): Caused by: java.lang.RuntimeException: takePicture failed
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.hardware.Camera.native_takePicture(Native Method)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.hardware.Camera.takePicture(Camera.java:1095)
04-15 18:24:42.316: E/AndroidRuntime(23331): at android.hardware.Camera.takePicture(Camera.java:1040)
04-15 18:24:42.316: E/AndroidRuntime(23331): at com.egnoita.ignoramus.CameraService.onClick(CameraService.java:50)
04-15 18:24:42.316: E/AndroidRuntime(23331): ... 14 more