Android 在后台服务中从照相机捕获图像

Android 在后台服务中从照相机捕获图像,android,service,background,camera,Android,Service,Background,Camera,我想使用相机拍摄图像作为服务。使用下面的代码,我从谷歌的某个地方得到了这个- import java.io.FileOutputStream; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.hardware.Cam

我想使用相机拍摄图像作为服务。使用下面的代码,我从谷歌的某个地方得到了这个-

import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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;

    boolean mPreviewRunning = false;


/** 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(getBaseContext());


    try {

        Camera.Parameters p = mCamera.getParameters();
        mCamera.setParameters(p);
        mCamera.startPreview();

        mCamera.takePicture(null, null, mPictureCallback);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        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 mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {
        Log.e("Callback TAG", "Here in jpeg Callback");

        if (imageData != null) {
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream("/sdcard/car_final/Image.jpg");
                outputStream.write(imageData);

                // Removed the finish call you had here
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (outputStream != null) try {
                    outputStream.close();
                } catch (IOException ex) {
                    // TODO Auto-generated catch block
                    ex.printStackTrace();
                }
            }

        }
    }
};
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
但我得到了以下例外-

请帮帮我。我已经找过了,但什么也没找到。
提前感谢。

我在运行我发现的类似代码时遇到了相同的错误

为了解决这个问题,我补充道

SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
mCamera.setPreviewTexture(st);

mCamera.startPreview()之前正如Viren Kheni所建议的。

当我运行我发现的类似代码时,我遇到了相同的错误

为了解决这个问题,我补充道

SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
mCamera.setPreviewTexture(st);

mCamera.startPreview()之前如Viren Kheni所建议。

此代码可能会在某些设备上运行。它使用了不受支持的功能:未连接到窗口的SurfaceView。实际上,可以从服务准备一个窗口上的视图。您是对的,此代码适用于某些设备,而不适用于其他设备。感谢您的回复。可能的-库副本处理后台图像捕获,并且易于使用。此代码可能会在某些设备上运行。它使用了不受支持的功能:未连接到窗口的SurfaceView。实际上,可以从服务准备一个窗口上的视图。您是对的,此代码适用于某些设备,而不适用于其他设备。感谢您的回复。可能重复的-库处理从背景图像捕获,它很容易使用。