Search code examples
blackberryfile-iooutputstreamimageringtone

Save media files to Blackberry SD card


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.


Solution

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