1

我在尝试让相机相应地工作时遇到了一些问题......相机演示适用于 8520 设备(有存储卡)但不适用于 9780 设备(没有存储卡)给出的错误

错误类 java.lang.ArrayOutOfBoundsException:索引 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")”,谁能告诉我是否你可以看到我的代码有什么问题???

4

1 回答 1

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

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

于 2012-11-02T13:59:21.413 回答