【问题标题】:Save media files to Blackberry SD card将媒体文件保存到黑莓 SD 卡
【发布时间】:2011-02-01 16:28:53
【问题描述】:

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

【问题讨论】:

    标签: blackberry file-io outputstream image ringtone


    【解决方案1】:

    保存任何东西都应该差不多。试试这样的:

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

    【讨论】:

    • 感谢您的回复。我理解其中的那一部分。但我不知道如何在我的文件中使用 write 语句。假设铃声和图片保存在“res”文件夹中。如何将资源媒体文件转换为字节(这样我就可以使用 write 语句)。图片为.jpg,铃声为.mp3。再次感谢。
    • 我想出了如何使用以下方法从图像资源中获取 byte[]:EncodedImage encImage = EncodedImage.getEncodedImageResource(file.jpg"); byte[] image = encImage.getData(); 但是,我不知道如何将我的 mp3 文件转换为字节 []。如果您能指出正确的方向,我将不胜感激。
    • 不确定我是否这样做了,但这个 URL 说 getClass().getResourceAsStream(filename) 是你最好的选择:docs.blackberry.com/en/developers/deliverables/11942/…
    【解决方案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){}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      相关资源
      最近更新 更多