0

I am trying to retrieve all images in the specific folder,For that I created a vector and move that images to that vector, the code I used is

imageNameVector.removeAllElements();
        try { 
                FileConnection fc = (FileConnection)Connector.open("file:///e:/Images", Connector.READ_WRITE);
                if(!fc.exists())
                {
                    fc.mkdir();
                }
                Enumeration filelist = fc.list("*.jpg", true);
                String filename;
                while(filelist.hasMoreElements()) {
                    filename = (String) filelist.nextElement();
                    imageNameVector.addElement(filename);
            }   
            fc.close();
        }
        catch (IOException ioe)
        {
            System.out.println("IOException: "+ioe.getMessage());            
        }
        catch (SecurityException se) {
            System.out.println("SecurityException: "+se.getMessage());            
        }

        System.out.checkError();



        return imageNameVector;
}

now I want to retrieve the elements from the vector and convert it to image,

   imageName  =  (String) imageNameVector.elementAt(1);
   try{
       image   = Image.createImage(imageName);
       }catch(Exception e){
            Alert alert  =   new Alert("Sngjfkgnlkjf")    ;
            alert.setString(""+imageName+e);
            display.setCurrent(alert);
       }

It shows an exception Abc,jpg cannot be read, Somebody pls help me to sort it out....

4

2 回答 2

0

createImage(String name) 方法用于从命名资源而不是文件创建图像。您可以使用 createImage(InputStream 流)、stream = Connector.openInputStream("file:///e:/Images/Abc.jpg") 等。

于 2013-08-23T13:31:01.150 回答
0

您不能直接访问 FileSystem 路径。使用下面的代码从文件系统访问加载图像

public static Image getImageFromPhone(String path) {
        try {
            FileConnection fconn = (FileConnection) Connector.open(path, Connector.READ);
            if (!fconn.exists()) {
                return null;
            }
            int length = (int)fconn.fileSize();
            byte[] data = new byte[length];
            DataInputStream din = fconn.openDataInputStream();
            din.readFully(data);
            fconn.close();
            return Image.createImage(data, 0, data.length);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
于 2013-08-28T09:47:34.120 回答