2

I want to make the application where I can get all the images no matter whether it is in phone or in external memory. I want to import all that images in my application. How can it be possible? I came to know that it is possible through file connection. But not getting exact idea.

gnat
  • 6,213
  • 108
  • 53
  • 73
shweta
  • 1,120
  • 1
  • 11
  • 25

2 Answers2

6
  1. Get all the file system roots using FileSystemRegistry.listRoots()
  2. Open connection to each root in turn using FileConnection fconn = (FileConnection)Connector.open(root)
  3. List the folder using fconn.list().
  4. For each entry in the list, if it ends with an image extension (file.getName().endsWith(".png") etc), then it's an image.
  5. If the entry is a folder (file.isDirectory() returns true) then use fconn.setFileConnection(folder) to traverse into that directory/
  6. Do the same recursively for all folders in all roots.
funkybro
  • 8,432
  • 6
  • 39
  • 52
  • thank for the reply but i am not able to open this application in my MOTORLA A1200.I had tried to installed the application but the application get close and not giving the list of the files – shweta Feb 18 '11 at 10:02
  • what url soul i write to import the image into my application.The image is in phone memory inside the folder name--MY IMAGES – shweta Feb 18 '11 at 11:22
  • I thought you wanted "all the images no mater where ever it is in phone or in external memory"? – funkybro Feb 18 '11 at 12:21
  • Yes i want that.How can i do that in j2me?? – shweta Feb 21 '11 at 05:20
  • 1
    By following the steps I've described above. – funkybro Feb 21 '11 at 08:02
  • Can you provide some code? I'm particularly interested in things like (a) in step 4, where do you get `file` - are you calling `open()` on a name from fconn.list()? (b) in step 5, when you fconn.setFileConnection(folder) and recurse, do you then have to fconn.setFileConnection back afterwards? I guess not ... – LarsH Jan 20 '15 at 03:46
2

Here is a code snippet I once used for my application. It more or less does the same in funkybros steps.

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

                }
Lucifer
  • 29,392
  • 25
  • 90
  • 143
ruben
  • 1,745
  • 5
  • 25
  • 47