2013-08-21 150 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; 
     } 
    }