从服务中获取错误打开相机

时间:2013-07-04 05:31:20

标签: android android-camera

我有一项活动正在该服务中启动服务我尝试捕获图像 并保存到SD卡。原始代码来源告诉我有什么问题http://easyandroidtutorials.blogspot.in/2012/09/capture-image-without-preview-as.html

错误

07-04 08:24:41.579: D/Camera(3036): app passed NULL surface

来自活动

Intent service = new Intent(context, CameraService.class);
    context.startService(service); 

服务代码

public class CameraService extends Service
{
//Camera variables
//a surface holder
private SurfaceHolder sHolder; 
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
/** Called when the activity is first created. */
@Override
public void onCreate()
{
    super.onCreate();

}
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    mCamera = Camera.open();
    SurfaceView sv = new SurfaceView(getApplicationContext());


    try {
        mCamera.setPreviewDisplay(sv.getHolder());
        parameters = mCamera.getParameters();

        //set camera parameters
        mCamera.setParameters(parameters);
        mCamera.startPreview();
        mCamera.takePicture(null, null, mCall);

    } catch (Throwable e) {
        e.printStackTrace();
    }


    //Get a surface
    sHolder = sv.getHolder();
    //tells Android that this surface will have its data constantly replaced
    sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}



Camera.PictureCallback mCall = new Camera.PictureCallback()
{

    public void onPictureTaken(byte[] data, Camera camera)
    {
        //decode the data obtained by the camera into a Bitmap

        FileOutputStream outStream = null;
        try{
            outStream = new FileOutputStream("/sdcard/Image.jpg");
            outStream.write(data);
            outStream.close();
        } catch (Throwable e){
            Log.d("CAMERA", e.getMessage());
        } 
    }
};


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

1 个答案:

答案 0 :(得分:0)

服务适用于应在后台执行的任务。您无法在服务中启动相机,因为相机需要预览表面。

相关问题