我创建了一项服务,我尝试在后台打开相机并拍摄一段时间。它正在运行并且正在拍摄一些照片,但是例如当倒计时器达到5秒时它会崩溃。 E / AndroidRuntime(10168):java.lang.RuntimeException:java.lang.RuntimeException:startPreview failed。
@SuppressWarnings({ "deprecation", "unused" })
public class PhotoTakingService extends Service {
static Camera camera = null;
@Override
public void onCreate() {
super.onCreate();
// takePhoto(this);
try {
camera = Camera.open(1);
} catch (Exception e) {
if (camera != null)
camera.release();
throw new RuntimeException(e);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showMessage("Service onStartCommand");
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
Log.i("seconds remaining: ",
String.valueOf(millisUntilFinished / 1000));
takePhoto(getApplicationContext(),
String.valueOf(millisUntilFinished / 1000));
showMessage("photoTaken at "
+ String.valueOf(millisUntilFinished / 1000));
}
public void onFinish() {
// camera.release();
//stopService(p);
}
}.start();
return Service.START_STICKY;
}
private static void takePhoto(final Context context, final String time) {
final SurfaceView preview = new SurfaceView(context);
SurfaceHolder holder = preview.getHolder();
// deprecated setting, but required on Android versions prior to 3.0
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new Callback() {
@Override
// The preview must happen at or after this point or takePicture
// fails
public void surfaceCreated(SurfaceHolder holder) {
showMessage("Surface created");
try {
showMessage("Opened camera");
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
throw new RuntimeException(e);
}
camera.startPreview();
showMessage("Started preview");
camera.takePicture(null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
showMessage("Took picture");
// decode the data obtained by the camera into a
// Bitmap
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(Environment
.getExternalStorageDirectory()
+ File.separator
+ "/traLaLaDownload/Images/Image"
+ time + ".jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
Log.d("CAMERA", e.getMessage());
} catch (IOException e) {
Log.d("CAMERA", e.getMessage());
}
//camera.release();
}
});
} catch (Exception e) {
if (camera != null)
camera.release();
throw new RuntimeException(e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(1,
1, // Must be at least 1x1
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0,
// Don't know if this is a safe default
PixelFormat.UNKNOWN);
// Don't set the preview visibility to GONE or INVISIBLE
wm.addView(preview, params);
}
private static void showMessage(String message) {
Log.i("Camera", message);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
日志
02-12 12:54:55.726:E / AndroidRuntime(12198):致命异常:主要 02-12 12:54:55.726:E / AndroidRuntime(12198):进程:com.duktech.tralala,PID:12198 02-12 12:54:55.726:E / AndroidRuntime(12198):java.lang.RuntimeException:java.lang.RuntimeException:startPreview failed 02-12 12:54:55.726:E / AndroidRuntime(12198):at com.duktech.tralala.utils.PhotoTakingService $ 2.surfaceCreated(PhotoTakingService.java:400) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.SurfaceView.updateWindow(SurfaceView.java:572) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.SurfaceView.access $ 000(SurfaceView.java:86) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:175) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1871) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5633) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.Choreographer.doCallbacks(Choreographer.java:574) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.Choreographer.doFrame(Choreographer.java:544) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.os.Handler.handleCallback(Handler.java:743) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.os.Handler.dispatchMessage(Handler.java:99) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.os.Looper.loop(Looper.java:136) 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.app.ActivityThread.main(ActivityThread.java:5196) 02-12 12:54:55.726:E / AndroidRuntime(12198):at java.lang.reflect.Method.invokeNative(Native Method) 02-12 12:54:55.726:E / AndroidRuntime(12198):at java.lang.reflect.Method.invoke(Method.java:515) 02-12 12:54:55.726:E / AndroidRuntime(12198):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 02-12 12:54:55.726:E / AndroidRuntime(12198):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 02-12 12:54:55.726:E / AndroidRuntime(12198):at dalvik.system.NativeStart.main(Native Method) 02-12 12:54:55.726:E / AndroidRuntime(12198):引起:java.lang.RuntimeException:startPreview失败 02-12 12:54:55.726:E / AndroidRuntime(12198):在android.hardware.Camera.startPreview(Native Method) 02-12 12:54:55.726:E / AndroidRuntime(12198):at com.duktech.tralala.utils.PhotoTakingService $ 2.surfaceCreated(PhotoTakingService.java:366)