【问题标题】:File connection+j2me文件连接+j2me
【发布时间】:2011-02-18 07:28:11
【问题描述】:

我想制作一个应用程序,无论它是在手机中还是在外部存储器中,我都可以获取所有图像。我想在我的应用程序中导入所有这些图像。怎么可能?我开始知道通过文件连接是可能的。但没有得到确切的想法。

【问题讨论】:

    标签: java-me jsr75


    【解决方案1】:
    1. 使用FileSystemRegistry.listRoots()获取所有文件系统根目录
    2. 依次使用FileConnection fconn = (FileConnection)Connector.open(root)打开到每个根的连接
    3. 使用fconn.list()列出文件夹。
    4. 对于列表中的每个条目,如果它以图片扩展名(file.getName().endsWith(".png") 等)结尾,那么它就是一张图片。
    5. 如果条目是文件夹(file.isDirectory() 返回 true),则使用 fconn.setFileConnection(folder) 遍历该目录/
    6. 对所有根目录中的所有文件夹递归执行相同操作。

    【讨论】:

    • 感谢您的回复,但我无法在 MOTORLA A1200 中打开此应用程序。我曾尝试安装该应用程序,但应用程序关闭且未提供文件列表
    • 我写什么 url 灵魂将图像导入我的应用程序。图像在文件夹名称内的手机内存中--我的图像
    • 我以为你想要“所有图像,无论它在手机中还是在外部存储器中”?
    • 是的,我想要那个。我如何在 j2me 中做到这一点??
    • 按照我上面描述的步骤。
    【解决方案2】:

    这是我曾经用于我的应用程序的代码 sn-p。它在 funkybros 步骤中或多或少是相同的。

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

    【讨论】:

    • 或多或少...特别是,它没有实现 funkybro 的步骤 5 或 6。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多