1

I want to take image using from camera as a service. And using following code, i have got this from somewhere on google-

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;
}

But i am getting following exception - Error message

Please help me. I have searched but not getting anything. Thanks in Advance.

Mayank Barnwal
  • 123
  • 1
  • 12
  • This code will probably run on some devices. It uses unsupported feature: a SurfaceView which is not connected to a Window. Actually, it is possible to prepare a View that will be on Window from a service. – Alex Cohn Sep 24 '15 at 22:17
  • You are right this code is working for some devices and is not working for some others. Thanks for the reply. – Mayank Barnwal Sep 24 '15 at 22:44
  • possible duplicate of [android - use camera from within background service](http://stackoverflow.com/questions/6901542/android-use-camera-from-within-background-service) – Alex Cohn Sep 25 '15 at 04:53
  • https://github.com/kevalpatel2106/android-hidden-camera - library handles image capture from background and it is easy to use. – Keval Patel Dec 09 '16 at 17:01

1 Answers1

0

I was getting the same error when I was running a similar code I found here.

To solve the issue, I added

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

before mCamera.startPreview(); as suggested by Viren Kheni here.

Community
  • 1
  • 1