Reputation: 78
I have an application where part of the functionality is user will generate a QR code and other users can scan and get contact details and add to their contact directly.
So i am encoding vCard details in below format:
String str =
"BEGIN:VCARD\n" +
"VERSION:3.0\r\n" +
"N:" + userDetailsModel.getLastName() + ";" + userDetailsModel.getFirstName() + ";;Mr;\r\n" +
"FN:" + "Mr." + userDetailsModel.getFirstName() + " " + userDetailsModel.getLastName() + "\r\n" +
"ORG:" + userDetailsModel.getCompany() + "\r\n" +
"TITLE:" + userDetailsModel.getDesignation() + "\r\n" +
"EMAIL:" + userDetailsModel.getEmail() + "\r\n" +
"TEL;CELL:" + userDetailsModel.getPhone() + "\r\n" +
"END:VCARD\r\n";
Then upon receiving this string after being scanned , i am writing this to a file and then starting an intent action with the file to open and add to contact. Below is my code:
VCard vCard = Ezvcard.parse(qrResultDataString).first();
writeToFile(qrResultDataString);
Intent intent = new Intent(Intent.ACTION_VIEW);
File path = new File(getApplicationContext().getExternalFilesDir(null), "contactFile.vcf");
try {
VCardWriter writer = new VCardWriter(path, VCardVersion.V3_0);
writer.write(vCard);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
Uri uri = Uri.parse(path.getPath());
intent.setDataAndType(uri, "text/x-vcard"); //storage path is path of your vcf file and vFile is name of that file.
startActivity(intent);
Ignore the EZvcard part.
As a result i am getting option to add to contact and when i select any contact app a toast is showing with text "couldn't import vCard". I am not getting any error message in logcat either. Please help.
Upvotes: 0
Views: 2100
Reputation: 11
I don't know if this is what you need
//GENERATE QR
private void generateQR(User user) {
String str =
"BEGIN:VCARD\n" +
"VERSION:3.0\r\n" +
"N:" + user.getSurname() + ";" + user.getName() + ";;Mr;\r\n" +
"FN:" + "Mr." + user.getName() + " " + user.getSurname() + "\r\n" +
"EMAIL:" + user.getEmail() + "\r\n" +
"TEL;CELL:" + user.getPhone() +"\r\n" +
"END:VCARD\r\n";
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bitMatrix = writer.encode(str, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
image.setImageBitmap(bmp);
} catch (WriterException e) {
e.printStackTrace();
}
}
Upvotes: 1
Reputation: 78
After much searching and trying different thing i finally found the answer. This was very silly. First thing is i forgot to use the file provider given i was targeting API level 24 plus. And other is i didn't use the below flag after using the provider.
Uri uri = FileProvider.getUriForFile(
ShareScreen.this,
"com.alphapublishing.digitalbusinesscard.provider",
path);
intent.setDataAndType(uri,"text/x-vcard");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Credit to this post : android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
Upvotes: 0