views:

1536

answers:

1

Hello, I have a BlackBerry application that needs to take pictures from the camera and send them to a server. In order to do this i invoke the native camera application and listen to the filesystem. Once an image is captured and saved as a new jpeg file i get notified, resume foreground control and go about my business. The problem starts occurring after the first time this cycle is completed because now when i decide to call the camera application again it is already opened, and now the user is seeing a thumbnail of the last picture that was taken and several buttons allowing him to manipulate/manage it. naturally what i want the user to see is a preview of what the camera is "seeing" before he snaps another photo as he did before.

I have thought of various ways to solve this including killing the camera app each time (I understand this cannot be done programatically?), sending CameraArguments when invoking the app (which appears to be useless), and now i was thinking a solution could be as simple generating a "Back" key event before switching back to my app which would theoretically dismiss the annoying edit screen. Could this really be done? and if not is there any other possible solution you may think of?

+2  A: 

A kind of hack...

  • start Camera App
  • in TimerTask check if Camera App started and if it need to be closed (some flag)
  • if yes, invoke it(so it will became active) and push ESC keypress event injection to close it

Take a look at this:

class Scr extends MainScreen {
    boolean killCameraApp = false;
    final String mCameraModuleName = "net_rim_bb_camera";
    final CameraArguments args = new CameraArguments();

    public Scr() {
     super();

     Timer timer = new Timer();
     timer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
       if (isCameraRunning() && killCameraApp) {
        getApplication().invokeAndWait(callCamera);
        getApplication().invokeAndWait(killCamera);
       }
      }
     }, 0, 100);
    }

    Runnable callCamera = new Runnable() {
     public void run() {
      callCamera();
     }

    };

    Runnable killCamera = new Runnable() {
     public void run() {
      injectKey(Characters.ESCAPE);
      killCameraApp = false;
     }
    };

    private boolean isCameraRunning() {
     boolean result = false;
     ApplicationManager appMan = 
          ApplicationManager.getApplicationManager();
     ApplicationDescriptor[] appDes = appMan.getVisibleApplications();
     for (int i = 0; i < appDes.length; i++) {
      result = mCameraModuleName.equalsIgnoreCase(appDes[i]
        .getModuleName());
      if (result)
       break;
     }
     return result;
    }

    private void callCamera() {
     Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, 
          new CameraArguments());
    }

    private void injectKey(char key) {
     KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
     inject.post();
    }

    protected void makeMenu(Menu menu, int instance) {
     menu.add(new MenuItem("start camera", 0, 0) {
      public void run() {
       callCamera();
       killCameraApp = false;
      }
     });
     menu.add(new MenuItem("kill app", 0, 0) {
      public void run() {
       killCameraApp = true;
      }
     });
     super.makeMenu(menu, instance);
    }
}

EDIT: Don't forget to set permissions for device release:
Options => Advanced Options => Applications => [Your Application] =>Edit Default permissions =>Interactions =>key stroke Injection

Max Gontar
Yes! looks very good, i'll try this approach. Thank you.
Orr Matarasso
You're welcome ))
Max Gontar