Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试将联系人转换为Vcard时发生FileNotFoundException_Android - Fatal编程技术网

Android 尝试将联系人转换为Vcard时发生FileNotFoundException

Android 尝试将联系人转换为Vcard时发生FileNotFoundException,android,Android,我正在尝试使用下面的代码将emulator上的联系人转换为VCard格式 AssetFileDescriptor afd=openAssetFileDescriptor(Contacts.CONTENT\u VCARD\u URI,“r”) 堆栈跟踪显示 java.io.FileNotFoundException无文件位于content://com.android.contacts/contacts/as_vcard 我们是否需要将文件附加到URI?在Android中,是否有其他方法将联系人转换

我正在尝试使用下面的代码将emulator上的联系人转换为VCard格式

AssetFileDescriptor afd=openAssetFileDescriptor(Contacts.CONTENT\u VCARD\u URI,“r”)

堆栈跟踪显示 java.io.FileNotFoundException无文件位于content://com.android.contacts/contacts/as_vcard


我们是否需要将文件附加到URI?在Android中,是否有其他方法将联系人转换为Vcard?

您必须遍历联系人数据库并分别调用openAssetFileDescriptor()。重要的一点是,您必须为每个联系人使用查找键,并使用URI.withAppendedPath()方法将其附加到内容卡片URI。

我也遇到了这个问题。这里有一个方法。首先,让用户选择他的联系人或以另一种方式获取联系人URI

获得contactUri后,您可以查找lookup_键,然后可以检索vcard。下面是我在获得contactUri后使用的代码(一种来自不同函数的复制粘贴,但应该可以工作)


我希望这个答案能帮助您:“[Android从Intent获取vCard数据][1]”[1]:
Cursor cursor = resolver.query(contactUri, new String[] {
    Contacts.LOOKUP_KEY
}, null, null, null);
FileInputStream input = null;

try {
    if (cursor.moveToFirst()) {
        return cursor.getString(0);
    } else return;

    AssetFileDescriptor afd = context.getContentResolver().openAssetFileDescriptor(
            Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey), "r");
    input = afd.createInputStream();

    int ch;
    StringBuffer strContent = new StringBuffer("");
    while ((ch = input.read()) != -1)
        strContent.append((char) ch);

    Log.d(TAG, strContent.toString());
} finally {
    cursor.close();
    if (input != null) {
        input.close();
    }
}