简体   繁体   English

在android中以编程方式创建vCard文件

[英]Creating vCard file programmatically in android

I am using the following code to read contacts and create a vcard file. 我使用以下代码来阅读联系人并创建一个vcard文件。

            String lookupKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
            Uri uri=Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);


            try {
                fd = cr.openAssetFileDescriptor(uri, "r");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
            try {
                fis = fd.createInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            byte[] buf = new byte[(int)fd.getDeclaredLength()];
            try {
                if (0 < fis.read(buf))
                {
                    vCard = new String(buf);
                    writer.write(vCard);
                    writer.write("\n");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

But while going through the list of contacts, I get the error: 但是在浏览联系人列表时,我收到错误:

ERROR/MemoryFile(284): MemoryFile.finalize() called while ashmem still open. ERROR / MemoryFile(284):当ashmem仍然打开时调用MemoryFile.finalize()。

And my generated .vcf file is missing some contacts and also does not end properly. 我生成的.vcf文件缺少一些联系人,也没有正确结束。

Can someone please tell me what is wrong with my code. 有人可以告诉我我的代码有什么问题。

You need close stream fis 你需要近距离fis

        try {
            fis = fd.createInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] buf = new byte[(int)fd.getDeclaredLength()];
        try {
            if (0 < fis.read(buf))
            {
                vCard = new String(buf);
                writer.write(vCard);
                writer.write("\n");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Close stream
        fis.close();

I had the same issue. 我遇到过同样的问题。 I used a open source android-vcard jar to write the contacts to vcard. 我使用开源android-vcard jar将联系人写入vcard。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM