我想制作一个应用程序,无论它是在手机中还是在外部存储器中,我都可以获取所有图像。我想在我的应用程序中导入所有这些图像。怎么可能?我开始知道通过文件连接是可能的。但没有得到确切的想法。
问问题
2064 次
2 回答
6
- 使用获取所有文件系统根目录
FileSystemRegistry.listRoots()
- 依次使用打开到每个根的连接
FileConnection fconn = (FileConnection)Connector.open(root)
- 使用 列出文件夹
fconn.list()
。 - 对于列表中的每个条目,如果它以图像扩展名(
file.getName().endsWith(".png")
等)结尾,那么它就是一个图像。 - 如果条目是文件夹(
file.isDirectory()
返回 true),则使用fconn.setFileConnection(folder)
遍历该目录/ - 对所有根目录中的所有文件夹递归地执行相同的操作。
于 2011-02-18T08:41:11.750 回答
2
这是我曾经用于我的应用程序的代码片段。它在 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();
}
于 2011-02-21T15:05:36.767 回答