Created
March 13, 2014 19:02
-
-
Save neetsdkasu/9534672 to your computer and use it in GitHub Desktop.
EZアプリ(J)でのmicroSDカードへのアクセス。 EZアプリ(J)エミュレータ上で動作確認済。ルートパスを取得してからファイルアクセスを行う。EZアプリ(J)エミュレータ上ではC:\Program Files\EZAPLI_J_EMULATOR\fcdata以下に保存される。※エミュレータ上のルートパスは sd/ ( file:///sd/ ) だけど実機上は不明。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import java.util.Enumeration; | |
import javax.microedition.io.Connector; | |
import javax.microedition.io.file.FileConnection; | |
import javax.microedition.io.file.FileSystemRegistry; | |
import javax.microedition.lcdui.Command; | |
import javax.microedition.lcdui.CommandListener; | |
import javax.microedition.lcdui.Displayable; | |
import javax.microedition.lcdui.Form; | |
import javax.microedition.lcdui.StringItem; | |
public class SampleForm extends Form implements CommandListener { | |
private Command cmd1; | |
private StringItem si1; | |
public SampleForm(String title) { | |
super(title); | |
si1 = new StringItem(null, "label"); | |
append(si1); | |
cmd1 = new Command("File", Command.SCREEN, 1); | |
addCommand(cmd1); | |
setCommandListener(this); | |
} | |
public void commandAction(Command arg0, Displayable arg1) { | |
if (arg0 == null) { | |
return; | |
} | |
if (cmd1 == arg0) { | |
// microSDカードの有無をチェック | |
String value = System.getProperty("com.kddi.dev.memcardstate"); | |
// System.out.println(value); | |
if ("1".equals(value) == false) { | |
si1.setText("no card"); | |
return; | |
} | |
si1.setText("wait"); | |
removeCommand(cmd1); // 多重実行を防ぐための怠慢なコード | |
Thread thread = new Thread(new Runnable() { | |
public void run() { | |
String root = null; | |
// ルートパスの取得、 | |
Enumeration listRoots = FileSystemRegistry.listRoots(); | |
while (listRoots.hasMoreElements()) { | |
String fileString = (String) listRoots.nextElement(); | |
// System.out.println(fileString); | |
root = "file:///" + fileString; | |
} | |
if (root != null) { | |
try { | |
// ファイルの作成 | |
FileConnection file = (FileConnection) Connector | |
.open(root + "myfile.txt"); | |
if (file.exists() == false) { | |
file.create(); | |
} | |
file.close(); | |
// ディレクトリの作成 | |
FileConnection dir = (FileConnection) Connector | |
.open(root + "mydir"); | |
if (dir.exists() == false) { | |
dir.mkdir(); | |
} | |
dir.close(); | |
} catch (Exception ex) { | |
// ex.printStackTrace(); | |
} | |
} | |
si1.setText("finish"); | |
addCommand(cmd1); | |
} | |
}); | |
thread.start(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment