Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 21
Registered: ‎06-10-2014
My Device: blackberry z10
My Carrier: T-mobile
Accepted Solution

[Help me] Save file from asset folder to mediacard

I've made a soundboard and i'm trying to add a feature to it so you can save the sound to the mediacard, but i can't find any information on how to do this

 

I made my App in QML and i'm not that good with c++

 

Could anyone help me out please?

Thank you

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: [Help me] Save file from asset folder to mediacard

Welcome to the forums.

 

While you can save files in QML you will need to delve in to C++ for copying or moving files, take a look at QFile...

 

http://developer.blackberry.com/native/documentation/cascades/device_platform/data_access/file_syste...

 

http://developer.blackberry.com/native/reference/cascades/qfile.html#copy

 

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 21
Registered: ‎06-10-2014
My Device: blackberry z10
My Carrier: T-mobile

Re: [Help me] Save file from asset folder to mediacard

[ Edited ]

Thank you very much!

Is there a chance you could give me an example code?
since my knowledge of c++ is not that great

I have an applicationui.cpp, applicationui.hpp and main.cpp file

Thanks again

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: [Help me] Save file from asset folder to mediacard

There is this piece of code taken from one of the samples which is used to copy a database file to the data folder but you should be able to adapt it...

 

bool DbHelper::copyDbToDataFolder(const QString databaseName)
{
    // Since we need read and write access to the data base it has
    // to be moved to a folder where we have access to it. First
    // we check if the file already exists (previously copied)
    QString dataFolder = QDir::homePath();
    QString newFileName = dataFolder + "/" + databaseName;
    QFile newFile( newFileName );

    if (!newFile.exists()) {
        // If the file is not already in the data folder we copy it from the
        // assets folder (read only) to the data folder (read and write).
        // Note that on a debug build you will be able to write to a data base
        // in the assets folder but that is not possible on a signed application.
        QString appFolder(QDir::homePath());
        appFolder.chop(4);
        QString originalFileName = appFolder + "app/native/assets/sql/" + databaseName;
        QFile originalFile( originalFileName );

        if (originalFile.exists()) {
            return originalFile.copy(newFileName);
        } else {
            qWarning() << "Failed to copy file, database file does not exist.";
            return false;
        }

    }

    return true;
}

 

Sorry not to write something more specific but I'm deep in to my own code at the moment.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: [Help me] Save file from asset folder to mediacard

If it's the sd card location you might struggle with take a look at the 1st and 3rd results in this search for more info...

 

http://supportforums.blackberry.com/t5/forums/searchpage/tab/message?filter=labels%2Clocation&locati...


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 21
Registered: ‎06-10-2014
My Device: blackberry z10
My Carrier: T-mobile

Re: [Help me] Save file from asset folder to mediacard

I think my knowledge of c++ is just too little to implement this into my app.

I've really no clue what to do when it comes to C++

I was able to change the path in the quotes example
thats it Smiley Tongue

QString newFileName = "/accounts/1000/removable/sdcard/" + fileName;
Developer
Posts: 21
Registered: ‎06-10-2014
My Device: blackberry z10
My Carrier: T-mobile

Re: [Help me] Save file from asset folder to mediacard

Could anyone please help me, i'm still trying to get this feature in my app

and i've no idea how to do this with c++

 

My app just got featured on crackberry.com and people would love to get this feature

http://crackberry.com/soundboard-doctor-who-will-bring-joy-whovians-device

 

BlackBerry World:

http://appworld.blackberry.com/webstore/content/56887855/

 

Thanks for the help

 

I'm just trying to figure out a way so people can use the sounds as ringtone for their phone

 

 

Highlighted
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: [Help me] Save file from asset folder to mediacard

Check the below url, you can get the code for writing and reading a file.

http://supportforums.blackberry.com/t5/Native-Development/Writing-reading-data-to-files-in-the-appli...
Developer
Posts: 21
Registered: ‎06-10-2014
My Device: blackberry z10
My Carrier: T-mobile

Re: [Help me] Save file from asset folder to mediacard

[ Edited ]

I am finally be able to copy the sounds to the folder but i still have a few problems

1. The path of the asset folder is a fixed path

2. when i try to use _App.SaveFile() in a listview i get the error (outside listview it works)

asset:///main.qml:385: ReferenceError: Can't find variable: _App

 

is it possible that the file name will be the value of the ListItemData? in my listview

i dont know how to get the value to c++

 

Thank you so much already!


Current code:

 

ApplicationUI.cpp

 

void ApplicationUI::SaveFile() {
    QString from = QDir::currentPath() + "/app/native/assets/sounds/10theme.ogg";
            QDir(QDir::currentPath() + "/shared/music/").mkdir("Doctor Who");
            QString to = QDir::currentPath() + "/shared/music/Doctor Who/10th-theme.ogg";
            QFile::copy(from, to);
}

 

 

 

ApplicationUI.hpp

Q_INVOKABLE void SaveFile();

 

 

Main.qml

                            contextActions: [
                                ActionSet {                                   
                                    actions: [
                                        ActionItem {
                                            title: "Save Sound"
                                                imageSource: "asset:///layout/ic_notification.png"
                                                ActionBar.placement: ActionBarPlacement.OnBar
                                                onTriggered: {
                                                    _App.SaveFile()
                                                }
                                            }
                                    ]
                                } // end of ActionSet  
                            ] // end of contextActions list

 

 

Developer
Posts: 21
Registered: ‎06-10-2014
My Device: blackberry z10
My Carrier: T-mobile

Re: [Help me] Save file from asset folder to mediacard

Solved!

 

ApplicatuinUI.cpp

 

void ApplicationUI::SaveFile(const QString normalString) {
    QString from = QDir::currentPath() + "/app/native/assets/sounds/" + normalString + ".ogg";
            QDir(QDir::currentPath() + "/shared/music/").mkdir("Doctor Who");
            QString to = QDir::currentPath() + "/shared/music/Doctor Who/" + normalString + ".ogg";
            QFile::copy(from, to);
}

 

ApplicatuinUI.hpp

 

    Q_INVOKABLE void SaveFile(const QString normalString);

 

Main.qml

 

_App.SaveFile(soundfile.text)