相机问题

时间:2012-11-02 07:19:27

标签: image blackberry camera

我试图让相机工作时遇到一些问题...相机演示在8520设备上工作(有存储卡)但在9780设备上无法工作(没有存储卡)给出的错误

  

ERROR类java.lang.ArrayOutOfBoundsException:index 0> = 0

我的代码示例:

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);
    }
}  
8520上的

(有一张存储卡)代码在9780(没有存储卡)上运行正常,代码在“System.out.println(”debug --- 1“)处停止”,任何人都可以请告诉我你是否可以看到我的代码有任何问题???

1 个答案:

答案 0 :(得分:0)

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

这是sollution,我的“SD卡可用性”代码只返回true,导致黑莓没有插入存储卡时图片无法保存。 @ Eugen Martynov请仔细阅读代码,你会看到它在那里:)