2012-09-30 23 views
3

我使用Intent.ACTION_SEND在我的應用程序中接收數據。我如何處理數據類型text/x-vcard的發送?Android從意向中獲取電子名片數據

當我使用intent.getStringExtra(Intent.EXTRA_TEXT)時,它會引發異常。

+0

你能看到,如果這是有幫助的http://stackoverflow.com/questions/4599567/android-vcard-string-to-contact – VendettaDroid

+0

沒有,我看了這個,那個文章是關於如何發送但不是如何接收。 –

+0

順便說一句,你怎麼給你發VCard?該代碼將會有所幫助。如果您使用的是uri,您可以嘗試intent.getData()。 – VendettaDroid

回答

8

這爲我工作:

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

uri.toString()給了我這個:

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, 

這是不以這種形式非常有用,但似乎那裏是堆棧的幾個職位有關獲取可用來自URI的路徑。

您可以通過電子名片的文字,下面好像是爲我工作:

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); 

的字符串將有回車,所以看到所有在logcat中的文本,確保搜索對於標籤(在上面的代碼中是「TAG」),所以你可以看到所有的行(參見下面的屏幕截圖)。

Screenshot of logcat

+0

是的,我收到了相同的,但我怎樣才能獲得像這裏http://en.wikipedia.org/wiki/VCard TEXT格式的電子名片數據。 –

+0

看我上面的編輯。 – hBrent

+0

謝謝,工作,但你忘記stream.close();'流讀後。 –