2013-08-21 146 views
0

我试图找回在特定文件夹中的所有图像,对于我创建了一个矢量并移动图像到矢量,我使用的代码是问题与加载图像

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

现在我想找回从向量元素并将其转换成图像,

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

它显示了一个异常ABC,JPG不能被读取,有人请帮我整理出来....

回答

0

的createImage(字符串名称)方法是f或者从命名资源创建图像,而不是从文件创建。您可以使用createImage(InputStream流),并使用stream = Connector.openInputStream(“file:/// e:/Images/Abc.jpg”)等。

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