Пишу свое первое приложение. Нужно, чтобы пользователь выбрал файл у себя на телефоне, а мидлет запомнил путь.
Нашел пример работы с файлами в SunJava Wireless Toolkit, убрал кое-что. Тестирую в эмуляторе - ок.
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
* Demonstration MIDlet for File Connection API. This MIDlet implements simple
* file browser for the filesystem available to the J2ME applications.
*
*/
public class FileBrowser extends MIDlet implements CommandListener {
private static final String[] attrList = { "Read", "Write", "Hidden" };
private static final String[] typeList = { "Regular File", "Directory" };
private static final String[] monthList =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
/* special string denotes upper directory */
private static final String UP_DIRECTORY = "..";
/* special string that denotes upper directory accessible by this browser.
* this virtual directory contains all roots.
*/
private static final String MEGA_ROOT = "/";
/* separator string as defined by FC specification */
private static final String SEP_STR = "/";
/* separator character as defined by FC specification */
private static final char SEP = '/';
private String currDirName;
private Command BUTT_Select = new Command("Выбрать", Command.OK, 1);
private Command BUTT_Exit = new Command("Выйти", Command.BACK, 2);
private Image dirIcon;
private Image fileIcon;
private Image[] iconList;
private String TheFile="";
public FileBrowser() {
currDirName = MEGA_ROOT;
try {
dirIcon = Image.createImage("/icons/dir.png");
} catch (IOException e) {
dirIcon = null;
}
try {
fileIcon = Image.createImage("/icons/file.png");
} catch (IOException e) {
fileIcon = null;
}
iconList = new Image[] { fileIcon, dirIcon };
}
public void startApp() {
try {
showCurrDir();
} catch (SecurityException e) {
Alert alert =
new Alert("Error", "You are not authorized to access the restricted API", null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Form form = new Form("Cannot access FileConnection");
form.append(new StringItem(null,
"You cannot run this MIDlet with the current permissions. " +
"Sign the MIDlet suite, or run it in a different security domain"));
form.addCommand(BUTT_Exit);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(alert, form);
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseApp() {
}
public void destroyApp(boolean cond) {
notifyDestroyed();
}
public void commandAction(Command c, Displayable d) {
if (c == BUTT_Select) {
List curr = (List)d;
final String currFile = curr.getString(curr.getSelectedIndex());
new Thread(new Runnable() {
public void run() {
if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
traverseDirectory(currFile);
} else {
TheFile="file://localhost/"+currDirName+currFile;
System.err.println("Файл выбран - "+TheFile);
}
}
}).start();
} else if (c == BUTT_Exit) {
destroyApp(false);
}
}
void showCurrDir() {
Enumeration e;
FileConnection currDir = null;
List browser;
try {
if (MEGA_ROOT.equals(currDirName)) {
e = FileSystemRegistry.listRoots();
browser = new List(currDirName, List.IMPLICIT);
} else {
currDir = (FileConnection)Connector.open("file://localhost/" + currDirName);
//currDir = (FileConnection)Connector.open("file://0:/" + currDirName);
e = currDir.list();
browser = new List(currDirName, List.IMPLICIT);
// not root - draw UP_DIRECTORY
browser.append(UP_DIRECTORY, dirIcon);
}
while (e.hasMoreElements()) {
String fileName = (String)e.nextElement();
if (fileName.charAt(fileName.length() - 1) == SEP) {
// This is directory
browser.append(fileName, dirIcon);
} else {
// this is regular file
browser.append(fileName, fileIcon);
}
}
browser.setSelectCommand(BUTT_Select);
browser.addCommand(BUTT_Exit);
browser.setCommandListener(this);
Display.getDisplay(this).setCurrent(browser);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
void traverseDirectory(String fileName) {
/* In case of directory just change the current directory
* and show it
*/
if (currDirName.equals(MEGA_ROOT)) {
if (fileName.equals(UP_DIRECTORY)) {
// can not go up from MEGA_ROOT
return;
}
currDirName = fileName;
} else if (fileName.equals(UP_DIRECTORY)) {
// Go up one directory
int i = currDirName.lastIndexOf(SEP, currDirName.length() - 2);
if (i != -1) {
currDirName = currDirName.substring(0, i + 1);
} else {
currDirName = MEGA_ROOT;
}
} else {
currDirName = currDirName + fileName;
}
showCurrDir();
}
}
Но в сожалению на моем Benq-Siemens пример не работает(запускается, показываем 0:\ 4:\, но при нажатии "Выбрать" ничего не происходит).
Помогите пожалуйста исправить код.