我正在使用以下代码读取联系人并创建 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();
}
但是在浏览联系人列表时,我收到错误:
ERROR/MemoryFile(284): MemoryFile.finalize() called while ashmem still open.
我生成的 .vcf 文件缺少一些联系人,也没有正确结束。
谁能告诉我我的代码有什么问题。
最佳答案
你需要关闭流 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();
关于android - 在 android 中以编程方式创建 vCard 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4845027/