我正在制作一个多媒体应用,允许用户保存壁纸和铃声。我知道我需要保存它们的路径是“SDCard / BlackBerry / ringtones / file.mp3”(或壁纸的“/ pictures”)。我搜索了论坛并发帖了几天,我发现的唯一一件事就是如何编写文本文件。现在,假设铃声和图片保存在项目资源文件夹中。如果你能提供任何意见,我将不胜感激。
答案 0 :(得分:4)
保存任何东西都应该是一样的。尝试这样的事情:
FileConnection fc;
try {
String fullFile = usedir + filename;
fc = (FileConnection) Connector.open(fullFile, Connector.READ_WRITE);
if (fc.exists()) {
Dialog.alert("file exists");
} else {
fc.create();
fileOS = fc.openOutputStream();
fileOS.write(raw_media_bytes, raw_offset, raw_length);
}
} catch (Exception x) {
Dialog.alert("file save error);
} finally {
try {
if (fileOS != null) {
fileOS.close();
}
if (fc != null) {
fc.close();
}
} catch (Exception y) {
}
}
usedir和filename是你的路径组件,raw_media_bytes是你的数据等等。
答案 1 :(得分:2)
感谢您的帮助cjp。以下是将资源mp3文件保存到SD卡的代码:
byte[] audioFile = null;
try {
Class cl = Class.forName("com.mycompany.myproject.myclass");
InputStream is = cl.getResourceAsStream("/" + audioClip);
audioFile = IOUtilities.streamToBytes(is);
try {
// Create folder if not already created
FileConnection fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/ringtones/");
if (!fc.exists())
fc.mkdir();
fc.close();
// Create file
fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/ringtones/" + audioClip, Connector.READ_WRITE);
if (!fc.exists())
fc.create();
OutputStream outStream = fc.openOutputStream();
outStream.write(audioFile);
outStream.close();
fc.close();
Dialog.alert("Ringtone saved to BlackBerry SDcard.");
} catch (IOException ioe) {
Dialog.alert(ioe.toString());
}
} catch (Exception e) {
Dialog.alert(e.toString());
}
正如cjp所指出的,以下是如何将图像资源保存到SD卡:
EncodedImage encImage = EncodedImage.getEncodedImageResource(file.jpg");
byte[] image = encImage.getData();
try {
// create folder as above (just change directory)
// create file as above (just change directory)
} catch(Exception e){}