Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
NGeldenhuys
Posts: 176
Registered: ‎04-12-2012
My Device: Bold 9780 & Curve 8250
Accepted Solution

Camera Problem

[ Edited ]

Hi all, I have run into a few problems when trying to get the camera to to work accordingly... The camera Demo Works on the 8520 device (Has a memory Card) but does not work on the 9780 device (Has No Memory Card) the error given "ERROR Class java.lang.ArrayOutOfBoundsException :index 0>=0"

 

My code Sample:

public class MyScreen extends MainScreen{
	 Player _p;
     VideoControl _videoControl;  
     FileConnection fileconn;
     String PATH;
     String GetfileName;
     LabelField GetPhotofileName = new LabelField("",LabelField.FOCUSABLE){
      	protected boolean navigationClick(int status, int time){
      		Dialog.alert("Clicked");
			return true;
      	}
     };
      
     public static boolean SdcardAvailabulity() {
    	 String root = null;
    	 Enumeration e = FileSystemRegistry.listRoots();
    	 while (e.hasMoreElements()) {
    		 root = (String) e.nextElement();
    		 if( root.equalsIgnoreCase("sdcard/") ) {
    		 }else if( root.equalsIgnoreCase("store/") ) {
    		 }
    	 }
    	 class MySDListener implements FileSystemListener {
    		 public void rootChanged(int state, String rootName) {
    			 if( state == ROOT_ADDED ) {
    				 if( rootName.equalsIgnoreCase("sdcard/") ) {
    				 }
    			 } else if( state == ROOT_REMOVED ) {
    			 }
    		 }
    	 }
    	 return true;
 	}
     protected boolean invokeAction(int action){
         boolean handled = super.invokeAction(action); 
         if(SdcardAvailabulity()){
               PATH = System.getProperty("fileconn.dir.memorycard.photos")+"Image_"+System.currentTimeMillis()+".jpg";//here "str" having the current Date and Time;
         } else {
        	// PATH = System.getProperty("file:///store/home/user/pictures/")+"Image_"+System.currentTimeMillis()+".jpg"; 
             PATH = System.getProperty("fileconn.dir.photos")+"Image_"+System.currentTimeMillis()+".jpg"; 
         }
         if(!handled){
             if(action == ACTION_INVOKE){   
                 try{                      
                     byte[] rawImage = _videoControl.getSnapshot(null);
                     System.out.println("----------1");
                     fileconn=(FileConnection)Connector.open(PATH);
                     System.out.println("----------2");
                     if(fileconn.exists()){
                         fileconn.delete();
                         System.out.println("----------3");
                     }
                     fileconn.create();
                     System.out.println("----------4");
                     OutputStream os=fileconn.openOutputStream();
                     System.out.println("----------5");
                     os.write(rawImage);
                     GetfileName =fileconn.getName();
                     System.out.println("----------6");
                     System.out.println("GetfileName----------"+GetfileName);
                     fileconn.close();
                     System.out.println("----------7");
                     os.close();
                     
                     Status.show("Image is Captured",200);
                     GetPhotofileName.setText(GetfileName);
                     System.out.println("----------8");
                     if(_p!=null)
                    	 _p.close();          
                     System.out.println("----------9");
                 }catch(Exception e){
                     if(_p!=null){
                    	 _p.close();
                     }
                     if(fileconn!=null){
                         try{
                             fileconn.close();
                         }catch (IOException e1){ 
                                    //if the action is other than click the trackwheel(means go to the menu options) then we do nothing;
                         }
                     }                      
                 }
             }
         }           
         return handled;                
     }
     public MyScreen(){
    	 setTitle("Camera App");
    	 try{
    		 System.out.println("Debug------------10");
    		 _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
    		 _p.realize();
    		 _videoControl = (VideoControl) _p.getControl("VideoControl");
    		 System.out.println("Debug------------11");
    		 if (_videoControl != null){
    			 Field videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
    			 _videoControl.setDisplayFullScreen(true);
    			 System.out.println("Debug------------12");
    			 _videoControl.setVisible(true);
    			 _p.start();
    			 System.out.println("Debug------------13");
    			 if(videoField != null){
    				 add(videoField);
    				 System.out.println("Debug------------14");
    			 }
    		 } 
    	 }catch(Exception e){
    		 if(_p!=null) {
    			 _p.close();
    		 }
    		 Dialog.alert(e.toString());
    	 }   
    	 add(GetPhotofileName);
     }
}

 on the 8520 (Has a Memory Card) the code works fine on the 9780 (Has no Memory Card) the the code stops at "System.out.println("debug---1")", can anyone please tell me if you can see any problem with my code???  

Developer
simon_hain
Posts: 16,347
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Camera Problem

i am not able to match your code with the camera demo application, could you specify which one you are using?

a method named SdcardAvailabulity that just returns true could maybe cause an error on a device where no sd card is available.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
NGeldenhuys
Posts: 176
Registered: ‎04-12-2012
My Device: Bold 9780 & Curve 8250

Re: Camera Problem

I just added in the info about the Camera Demo for interest sake, Sorry about not clarifying that. I will look in to the method named SdcardAvailabulity that just returns true, will get back to you in a bit and let you know if that resolves my problem, thank you.

Trusted Contributor
NGeldenhuys
Posts: 176
Registered: ‎04-12-2012
My Device: Bold 9780 & Curve 8250

Re: Camera Problem

Thank you Simon_hain, that was the problem. I would have never picked it up.

 public static boolean SdcardAvailabulity() {
    	 String root = null;
    	 Enumeration e = FileSystemRegistry.listRoots();
    	 while (e.hasMoreElements()) {
    		 root = (String) e.nextElement();
    		 if( root.equalsIgnoreCase("sdcard/") ) {
    			 return true;
    		 }else if( root.equalsIgnoreCase("store/") ) {
    			 return false;
    		 }
    	 }
    	 class MySDListener implements FileSystemListener {
    		 public void rootChanged(int state, String rootName) {
    			 if( state == ROOT_ADDED ) {
    				 if( rootName.equalsIgnoreCase("sdcard/") ) {
    				 }
    			 } else if( state == ROOT_REMOVED ) {
    			 }
    		 }
    	 }
    	 return true;
 	}

 Here is the correction.