【发布时间】:2011-02-18 07:28:11
【问题描述】:
我想制作一个应用程序,无论它是在手机中还是在外部存储器中,我都可以获取所有图像。我想在我的应用程序中导入所有这些图像。怎么可能?我开始知道通过文件连接是可能的。但没有得到确切的想法。
【问题讨论】:
我想制作一个应用程序,无论它是在手机中还是在外部存储器中,我都可以获取所有图像。我想在我的应用程序中导入所有这些图像。怎么可能?我开始知道通过文件连接是可能的。但没有得到确切的想法。
【问题讨论】:
FileSystemRegistry.listRoots()获取所有文件系统根目录
FileConnection fconn = (FileConnection)Connector.open(root)打开到每个根的连接
fconn.list()列出文件夹。file.getName().endsWith(".png") 等)结尾,那么它就是一张图片。file.isDirectory() 返回 true),则使用 fconn.setFileConnection(folder) 遍历该目录/【讨论】:
这是我曾经用于我的应用程序的代码 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();
}
【讨论】: