1

我正在创建一个多媒体应用程序,允许用户保存壁纸和铃声。我知道我需要将它们保存到的路径是“SDCard/BlackBerry/ringtones/file.mp3”(或用于壁纸的“/pictures”)。我已经搜索了几天的论坛和帖子,我唯一发现的是如何编写文本文件。现在,假设铃声和图片保存在项目资源文件夹中。如果您能提供任何意见,我将不胜感激。

4

2 回答 2

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 是您的数据,等等。

于 2011-02-01T17:12:33.173 回答
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){}
于 2011-02-01T22:46:32.903 回答