Search code examples
galleryblackberry-10

Blackberry 10 scan gallery


I am working on content migration application. I have to migrate contacts, calendars, media from Blackberry device to Android device. Contacts and Calendars I have done.

I used below snip of code for Contacts

ContactService contactService;
ContactListFilters filters;
filters.setLimit(0);
QList<Contact> contactList = contactService.contacts(filters);

And below for Calendars

CalendarService calendarService;
EventSearchParameters searchParams;
searchParams.setStart(QDateTime(QDate(1918, 01, 01), QTime(00,00,00)));
searchParams.setEnd(QDateTime(QDate(2118, 12, 31), QTime(00,00,00)));
QList<CalendarEvent> eventList = calendarService.events(searchParams);

Its working fine.

Now, I have to lookup media in device i.e get media path based on type say all Image, all Audio and all Video present in device.

Then with those path have to create a output stream and send it to destination.


Solution

  • I've heard you can query the media SQL database available on every device, but I've never done it myself so I can't help on that one. The db file is located at /db/mmlibrary.db for media files stored on device and at /db/mmlibrary_SD.db for media files stored on SD card.

    Otherwise, you can recursively navigate through the device and keep a global list of file paths. Note that doing so can take a long time, for my personal device it took 25 seconds to recursively go through all folders and find 186 audio files, 5127 picture files and 28 video files. You might want to execute this code in a separate thread to avoid blocking UI.

    #include "applicationui.hpp"
    
    #include <bb/cascades/Application>
    #include <bb/cascades/QmlDocument>
    #include <bb/cascades/AbstractPane>
    
    #include <QFileInfo>
    #include <QDir>
    
    using namespace bb::cascades;
    
    const QStringList audioFileExtensions = QStringList() << "mp3" << "wav";
    const QStringList pictureFileExtensions = QStringList() << "bmp" << "gif" << "ico" << "jpg" << "jpeg" << "png" << "tiff";
    const QStringList videoFileExtensions = QStringList() << "avi" << "mkv" << "mp4" << "mpeg";
    
    ApplicationUI::ApplicationUI() :
            QObject()
    {
        QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
        qml->setContextProperty("_app", this);
    
        AbstractPane *root = qml->createRootObject<AbstractPane>();
        Application::instance()->setScene(root);
    }
    
    //Declared as public Q_INVOKABLE in hpp
    void ApplicationUI::findMediaFiles(QString parentFolder) {
        QDateTime start = QDateTime::currentDateTime();
        qDebug() << "findMediaFiles() started in" << parentFolder;
    
        //Those 3 QStringList are declared as private variables in hpp
        audioFilePaths.clear();
        pictureFilePaths.clear();
        videoFilePaths.clear();
    
        if (parentFolder.isEmpty()) {
            parentFolder = QString(getenv("PERIMETER_HOME")) + "/shared";
        }
    
        findMediaFilesRecursively(parentFolder);
    
        qDebug() << audioFilePaths.size() << audioFilePaths;
        qDebug() << pictureFilePaths.size() << pictureFilePaths;
        qDebug() << videoFilePaths.size() << videoFilePaths;
        qDebug() << "Took" << start.secsTo(QDateTime::currentDateTime()) << "seconds";
    }
    
    //Declared as private in hpp
    void ApplicationUI::findMediaFilesRecursively(QString parentFolder) {
        QDir dir(parentFolder);
        dir.setFilter(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks);
        dir.setSorting(QDir::DirsFirst);
        QFileInfoList fileInfoList = dir.entryInfoList();
    
        foreach(QFileInfo fileInfo, fileInfoList) {
            if (fileInfo.isDir()) {
                findMediaFilesRecursively(fileInfo.absoluteFilePath());
                continue;
            }
    
            QString extension = fileInfo.fileName().split(".").last();
    
            if (audioFileExtensions.contains(extension, Qt::CaseInsensitive)) {
                audioFilePaths.append(fileInfo.absoluteFilePath());
            }
            else if (pictureFileExtensions.contains(extension, Qt::CaseInsensitive)) {
                pictureFilePaths.append(fileInfo.absoluteFilePath());
            }
            else if (videoFileExtensions.contains(extension, Qt::CaseInsensitive)) {
                videoFilePaths.append(fileInfo.absoluteFilePath());
            }
        }
    }