Android从Intent获取vCard数据
作者:互联网
我使用Intent.ACTION_SEND在我的应用程序中接收数据.如何处理数据类型text / x-vcard的发送?
当我使用intent.getStringExtra(Intent.EXTRA_TEXT)时,它会抛出异常.
解决方法:
这对我有用:
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获取可用路径的帖子.
要从vcard获取文本,以下似乎对我有用:
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”),因此您可以看到所有行(请参阅下面的屏幕截图).
标签:vcard,android,android-intent 来源: https://codeday.me/bug/20190723/1511667.html