c++qtcontactsblackberry-10

Blackberry 10 contact pic to byte array


I am working on migration application. I have to transfer blackberry 10 contacts to android.

I am getting problem in transferring contact pic. I am getting the uri of the pic, create file and try to read the bytes.

ContactPhoto contactPhoto = contact.primaryPhoto();
QString photo = contactPhoto.originalPhoto();
//photo = file:///accounts/1000/pimdata/_startup_data/contacts/2/img-tnqpx0.jpg
if (!photo.isEmpty()){
    QFile file(photo);
    if (file.open(QIODevice::ReadOnly)) {
       qDebug() <<"file.readAll() IF" <<file.readAll() <<endl;
    }else{
       qDebug() <<"file.readAll() ELSE" <<endl;
    }
    vcardString += "PHOTO;JPEG;ENCODING=BASE64:" + (file.readAll() + "\n");
}

But else part of the below snip of code is executing

if (file.open(QIODevice::ReadOnly)) {
    qDebug() <<"file.readAll() IF" <<file.readAll() <<endl;
}else{
    qDebug() <<"file.readAll() ELSE" <<endl;
}

How I read bytes from below uri

file:///accounts/1000/pimdata/_startup_data/contacts/2/img-tnqpx0.jpg


Solution

  • As I suspected, removing file:// from the url works.

    Here's the code I used to test :

        bb::pim::contacts::ContactPhoto contactPhoto = contact.primaryPhoto();
        QString photo = contactPhoto.originalPhoto();
        if (!photo.isEmpty()){
            QFile file(photo.remove("file://"));
            if (file.open(QIODevice::ReadOnly)) {
               qDebug() <<"file.readAll() IF" <<file.readAll() <<endl;
            }else{
               qDebug() <<"file.readAll() ELSE" <<endl;
            }
        }
    

    Please note that you need to leave one / in front of the url, so your picture url shared in OP would look like :

    /accounts/1000/pimdata/_startup_data/contacts/2/img-tnqpx0.jpg
    

    Also, if your goal is to create a VCard vcf file, you don't need to manually create the VCard file content, don't even need to read the bytes of the photo file, the contactToVCard function will do that for you.

    QByteArray vcard = contactService.contactToVCard(contact.id(), bb::pim::contacts::VCardPhotoEncoding::BASE64, -1);
    qDebug() << "vcard:" << vcard;