Domanda
Voglio rendere l'applicazione in cui posso ottenere tutte le immagini, indipendentemente dal fatto che sia nel telefono o nella memoria esterna.Voglio importare tutte le immagini nella mia applicazione.Come può essere possibile?Sono venuto a sapere che è possibile tramite connessione file.Ma non ottenere idea esatta.
Soluzione
- Get all the file system roots using
FileSystemRegistry.listRoots()
- Open connection to each root in turn using
FileConnection fconn = (FileConnection)Connector.open(root)
- List the folder using
fconn.list()
. - For each entry in the list, if it ends with an image extension (
file.getName().endsWith(".png")
etc), then it's an image. - If the entry is a folder (
file.isDirectory()
returns true) then usefconn.setFileConnection(folder)
to traverse into that directory/ - Do the same recursively for all folders in all roots.
Altri suggerimenti
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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow