Toshiro007 Toshiro007 - 1 month ago 21
Android Question

How to get String Name in VCF Format String Android via CamCard

So I use CamCard API to get the VCard Format String, and this string appears from BEGIN:VCARD to END:VCARD



From BEGIN:VCARD and END:VCARD, how do I get the name, phone number, e-mail in String separately? Any reference that what should I do?

Answer
    @Override
    public void onQRCodeRead(final String text, PointF[] points) {
        pointsOverlayView.setPoints(points);

        if (text.length() > 0 && !text.isEmpty()) {
            resultTextView.setText(text);
            qrCodeReaderView.stopCamera();
            btnSaveToContact.setVisibility(View.VISIBLE);

            btnSaveToContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (checkandRequestPermission()) {

                        VCard vCard = Ezvcard.parse(text).first();

                        File vcfFile = new File(getActivity().getExternalFilesDir(null), "generated.vcf");

                        VCardWriter writer = null;
                        try {
                            writer = new VCardWriter(vcfFile, vCard.getVersion());
                            writer.write(vCard);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (writer != null) {
                                try {
                                    writer.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        Intent i = new Intent();
                        i.setAction(android.content.Intent.ACTION_VIEW);
                        i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
                        startActivity(i);
                    }
                }
            });
                model = new Model();
                model.setQrText(resultTextView.getText().toString());
                model.setDate(getDateTime().toString());
                realm.beginTransaction();
                realm.copyToRealm(model);
                realm.commitTransaction();
        }
    }

here "text" is my qr code result. what i m doing is to making one .vcf file and storing my qrcode result in it. and after that VCardWriter will do its work.. it will seprate your result text regarding Contact application. and its automatically store in Contact application.

or if you want each separate result as a string. this is how you can get it.

 String name = vCard.getFormattedName().getValue();
 String email = vCard.getEmails().get(0).getValue();
 String address = vCard.getAddresses().get(0).getStreetAddress() + vCard.getAddresses().get(0).getCountry();
 String birthday = vCard.getBirthday().getText().toString();
 String telephone = vCard.getTelephoneNumbers().toString();

you need to add this dependency first:

compile 'com.googlecode.ez-vcard:ez-vcard:0.10.0'