我试图偷偷点击某人的照片。我正面向相机和相机对象。但是,当我尝试点击takepicture时,我的应用程序崩溃了。
package com.example.xx.xx;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
public final static String DEBUG_TAG = "MakePhotoActivity";
private Camera camera;
private int cameraId = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
// 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 {
getCamera();
camera.takePicture(null, null,
new PhotoHandler(this));
}
}
}
private void getCamera()
{
releaseCameraAndPreview();
camera = Camera.open(cameraId);
Log.d(DEBUG_TAG,"found the camera.");
Log.d(DEBUG_TAG,"Camera is" + camera);
camera.startPreview();
}
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;
}
}
Log.d(DEBUG_TAG,"the camera id is" + cameraId);
return cameraId;
}
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
@Override
protected void onPause() {
if (camera != null) {
camera.release();
camera = null;
}
super.onPause();
}
}
我到这里的输出是:
04-29 06:21:29.994 3608-3608/com.example.khan.hiddencamera D/MakePhotoActivity: Camera found
04-29 06:21:29.994 3608-3608/com.example.khan.hiddencamera D/MakePhotoActivity: the camera id is1
04-29 06:21:30.274 3608-3608/com.example.khan.hiddencamera D/MakePhotoActivity: found the camera.
04-29 06:21:30.274 3608-3608/com.example.khan.hiddencamera D/MakePhotoActivity: Camera isandroid.hardware.Camera@41b2b330
04-29 06:21:30.284 3608-3608/com.example.khan.hiddencamera W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416e9e30)
04-29 06:21:30.314 3608-3608/com.example.khan.hiddencamera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.khan.hiddencamera, PID: 3608
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3855)
at android.view.View.performClick(View.java:4480)
at android.view.View$PerformClick.run(View.java:18609)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3850)
at android.view.View.performClick(View.java:4480)
at android.view.View$PerformClick.run(View.java:18609)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1709)
at android.hardware.Camera.takePicture(Camera.java:1626)
at com.example.khan.hiddencamera.MainActivity.onClick(MainActivity.java:43)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3850)
at android.view.View.performClick(View.java:4480)
at android.view.View$PerformClick.run(View.java:18609)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的代码似乎没问题。你在清单中添加了权限吗?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
答案 1 :(得分:0)
您需要定义相机处理程序回调方法
void onPictureTaken(byte[] data, Camera camera)
https://developer.android.com/reference/android/hardware/Camera.PictureCallback.html
答案 2 :(得分:0)
5. Important:将完全初始化的SurfaceHolder传递给setPreviewDisplay(SurfaceHolder)。没有表面,相机将无法开始预览。
只有在预览显示准备就绪后(调用surfaceCreated())回调后,才可以调用takePicture()。