Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get vCard data from Intent

I use Intent.ACTION_SEND for receiving data in my app. How can I process sending of data type text/x-vcard?

When I use intent.getStringExtra(Intent.EXTRA_TEXT), it's throws an exception.

like image 805
Dmytro Zarezenko Avatar asked Sep 30 '12 06:09

Dmytro Zarezenko


1 Answers

This worked for me:

Uri uri = (Uri) intent.getExtras().get(Intent.EXTRA_STREAM);

uri.toString() gave me this:

content://com.android.contacts/contacts/as_multi_vcard/2876r7-2A962A902A9045494F35.1957ifcf79ef7e82a0b99%3A2876r8-315551534945354F312D4F35.1957ieb4f1a6b0fcb82b9%3A2876r9-57493D3135452D3D43.1957ief585d48b64784c0%3A2876r10-4F35373D4343474959.1957if0a70009608fae23%3A2876r11-313B3531412F2D432D473135.1957i39c8fd1b6ef69e84%3A1957i51f47cecc7822f19.4035i611323cd8d55aa13%3A4035i8.4035i74aa09b30a019fa1%3A4035i2c0b359c8a47878e%3A4035i6d3c40138feef64b%3A4035i7d583f5488e47e41%3A4035ia6.4035i5%3A4035i6029e050898a86d3%3A4035i77caf53c89b4da48%3A4035i2a11745a8f3d5667%3A4035i9%3A4035i59ab32fd0fa955a9%3A4035i7391f1908a38ed1b%3A4035i6769848b08a214b0%3A4035i5a7c03c88bd1ba9e%3A4035i126427da8dfc0763%3A4035i35003ea5093abeb0%3A4035i1906758a8e16ca3a%3A4035i59a7953d883a78bf%3A4035i64249c098820452a%3A4035i48145af48ed78ebc%3A4035i2093d7568be9dff6%3A4035i469c62120db59d35.4035i388017020fbfb07e%3A4035i47e19c048aa116d7%3A239i3%3A239i2%3A239i249%3A239i248%3A239i1,

which is not terribly useful in that form, but there seem to be several posts on stack about getting a usable path from a URI.

To get the text from the vcard, the following seems to be working for me:

ContentResolver cr = getContentResolver();
InputStream stream = null;
try {
    stream = cr.openInputStream(uri);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

StringBuffer fileContent = new StringBuffer("");
int ch;
try {
    while( (ch = stream.read()) != -1)
      fileContent.append((char)ch);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
String data = new String(fileContent);
Log.i("TAG", "data: " + data);

The string will have carriage returns, so to see all of the text in the logcat, make sure to search for the tag (in the above code it's "TAG"), so you see all the lines (see my screenshot below).

Screenshot of logcat

like image 134
hBrent Avatar answered Sep 29 '22 00:09

hBrent