将媒体文件保存到黑莓 SD 卡

发布于 2024-10-15 22:41:47 字数 175 浏览 7 评论 0原文

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

I am creating a multimedia app that allows the user to save wallpapers and ringtones. I know the path I need to save them to is "SDCard/BlackBerry/ringtones/file.mp3" (or "/pictures" for wallpapers). I have searched forums and post for a couple days and the only thing I found was how to write text files. For now, assume that the ringtones and pictures are saved in the projects resource folder. If you could provide any input, I would greatly appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

近箐 2024-10-22 22:41:47

保存任何东西都应该是一样的。尝试这样的事情:

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

Saving anything should be about the same. Try something like this:

    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 and filename are your path components, raw_media_bytes is your data, etc etc.

愚人国度 2024-10-22 22:41:47

谢谢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){}

Thanks for your help cjp. Here is the code to saving a resource mp3 file to a sd card:

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());
}

As cjp pointed out, here is how to save an image resource to a SD card:

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){}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文