Creating vCard file programmatically in android

I use the following code to read contacts and create a vcard file.

            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 when viewing the contact list, I get an error message:

ERROR / MemoryFile (284): Called MemoryFile.finalize () ashmem is still open.

And my generated .vcf file doesn’t have some contacts, nor does it end up properly.

Can someone please tell me what is wrong with my code.

+3
source share
2 answers

You need fis private thread

        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();
+1
source

I had the same problem. I used the open android-vcard jar to write contacts to vcard.

0
source

Source: https://habr.com/ru/post/1788849/


All Articles