我正在制作一个应用程序来拍摄背景图片。当我在三星S3设备上尝试这一切时,一切正常,我没有任何错误,但当我尝试使用三星S4时,我收到以下内容:
04-05 15:07:56.379 9351-9351/com.parse.starter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.parse.starter, PID: 9351
java.lang.RuntimeException: Unable to start service com.parse.starter.camera.TakePhoto@42c6fc48 with Intent { cmp=com.parse.starter/.camera.TakePhoto }: java.lang.RuntimeException: takePicture failed
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2877)
at android.app.ActivityThread.access$2200(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
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:1341)
at android.hardware.Camera.takePicture(Camera.java:1286)
at com.parse.starter.camera.TakePhoto.run(TakePhoto.java:69)
at com.parse.starter.camera.TakePhoto.onStartCommand(TakePhoto.java:42)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2860)
at android.app.ActivityThread.access$2200(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
这是我用来拍照的代码:(它的服务不是活动)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
id = ParseInstallation.getCurrentInstallation().getObjectId();
// do we have a camera?
type = intent.getBooleanExtra("Location", false);
if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
} else {
camera = Camera.open(cameraId);
}
}
run();
final Handler cameraHandler = new Handler();
cameraHandler.postDelayed(new Runnable() {
@Override
public void run() {
onPause();
if (type)stopSelf();
}
}, 5000)
return 1;
}
public void run() {
camera.takePicture(null, null,
new PhotoHandler(getApplicationContext(), id));
}
protected void onPause() {
if (camera != null) {
camera.release();
camera = null;
}
}
和PhotoHandler.java
:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d(TakePhoto.DEBUG_TAG, "Can't create directory to save image.");
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (Exception error) {
}
}
private File getDir() {
File sdDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "Camera");
}
这是我在S3上运行时的日志:
04-05 15:41:35.690 20278-20278/com.parse.starter I/PUSH﹕ Received
04-05 15:41:35.710 20278-20278/com.parse.starter D/MakePhotoActivity﹕ Camera found
04-05 15:41:36.140 20278-20278/com.parse.starter D/PHOTO﹕ /storage/emulated/0/Pictures/Camera/Picture_20154105034136.jpg
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<service android:name=".camera.TakePhoto" />
答案 0 :(得分:0)
当您致电camera.takePicure()
此方法仅在预览处于活动状态时(startPreview()之后)有效。拍摄图像后将停止预览;如果呼叫者想要重新开始预览或拍摄更多照片,则必须再次调用startPreview()。不应在start()和stop()之间调用它。
我没有看到你在任何地方使用预览?
您需要startPreview
http://developer.android.com/reference/android/hardware/Camera.html#startPreview()
我看到你正试图在后台拍照(所以不要向用户展示预览)。你可以做到这一点,你只需要设置一个虚拟表面,我在这里解释如何做到这一点,你只需忽略关于面部检测的一点。
http://blog.blundell-apps.com/tut-front-camera-face-detection-explained/
修复上面的代码:
1)创建一个显示预览的DummySurface(但用户不会看到这个)。 https://github.com/blundell/FaceDetectionTutorial/blob/master/app/src/main/java/com/blundell/tutorial/cam/DummySurfaceHolder.java
2)在DummySurface
上设置camera
并致电startPreview
camera.setPreviewDisplay(new DummyHolder());
camera.startPreview();