2011-02-18 65 views
2

我想让应用程序可以获取所有图像,无论是在手机还是在外部存储器中。我想在我的应用程序中导入所有图像。怎么可能?我知道可以通过文件连接。但没有得到确切的想法。文件连接+ j2me

回答

6
  1. 使用到每一根依次使用FileConnection fconn = (FileConnection)Connector.open(root)
  2. 使用fconn.list()文件夹列表FileSystemRegistry.listRoots()
  3. 打开连接获取所有的文件系统的根。
  4. 对于列表中的每个条目,如果以图像扩展名(file.getName().endsWith(".png")等)结尾,则它是图像。
  5. 如果条目是一个文件夹(file.isDirectory()返回true),然后使用fconn.setFileConnection(folder)遍历到该目录/
  6. 做同样的递归在所有根的所有文件夹。
+0

感谢您的答复,但我不能够在我MOTORLA A1200.I打开这个应用程序曾试图安装的应用程序,但应用程序亲近,而不是让这些文件 – shweta 2011-02-18 10:02:07

+0

什么网址灵魂我写的名单将图像导入到我的应用程序中。图像位于手机内存中的文件夹名称 - 我的图像 – shweta 2011-02-18 11:22:59

2

这是我曾经用于我的应用程序的代码片段。它或多或少在时髦的步骤中也是如此。

protected void showFiles() { 
     if (path == null) { 
      Enumeration e = FileSystemRegistry.listRoots(); 
      path = DATAHEAD; //DATAHEAD = file:/// 
      setTitle(path); 
      while (e.hasMoreElements()) { 
       String root = (String) e.nextElement(); 
       append(root, null); 
      } 
      myForm.getDisplay().setCurrent(this); 
     } else { 
         //this if-else just sets the title of the Listing Form 
         if (selectedItem != null) { 
          setTitle(path + selectedItem); 
         } 
         else { 
          setTitle(path); 
         } 
      try { 
// works when users opens a directory, creates a connection to that directory 
       if (selectedItem != null) { 
        fileConncetion = (FileConnection) Connector.open(path + selectedItem, Connector.READ); 
       } else // works when presses 'Back' to go one level above/up 
       { 
        fileConncetion = (FileConnection) Connector.open(path, Connector.READ); 
       } 
// Check if the selected item is a directory 
       if (fileConncetion.isDirectory()) { 
        if (selectedItem != null) { 
         path = path + selectedItem; 
         selectedItem = null; 
        } 
        //gathers the directory elements 
        Enumeration files = fileConncetion.list(); 
        while (files.hasMoreElements()) { 
         String file = (String) files.nextElement(); 
         append(file, null); 
        } 
// 
        myForm.getDisplay().setCurrent(this); 
        try { 
         if (fileConncetion != null) { 
          fileConncetion.close(); 
          fileConncetion = null; 
         } 
        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 
       }//if (fileConncetion.isDirectory()) 
       else { 
        System.out.println(path); 
        //if it gets a file then calls the publishToServer() method 
        myForm.publishToServer(); 

       }