【发布时间】:2017-06-20 10:22:02
【问题描述】:
在 Android 7 中,getContentResolver().openAssetFileDescriptor(vCardUri, "r") 返回 AssetFileDescriptor,声明长度为 -1,由 getDeclaredLength() 返回。
尝试将联系人作为 vcard 导出到 vcf 文件中。我试过的代码如下
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] b = new byte[(int)fd.getDeclaredLength()];
fis.read(b);
以上代码在 Android 6 或更低版本中完美运行。但是当使用 Android 7 运行时,创建 byte[] 的行导致 NegativeByteArraySizeException,因为声明的Length 为 -1。
当我调试下载 Android 7 的源代码时,我发现了这个问题。
任何一种健康都是非常可观的。
【问题讨论】:
-
试试
getLength() -
我得到的 @pskink AssetFileDescriptor 的长度属性为 -1。我尝试了 getLength(),结果与以前相同 - NegativeByteArraySizeException
-
那么干脆不要使用它:而是在循环中读取你的数据
-
@pskink 您看待问题的方式与我看待问题的方式非常不同。你向我展示了我的解决方案。我得到了link 的帮助。谢谢。那么,我应该发布我的解决方案吗?或者你有什么建议?
-
随意发布您的解决方案作为答案
标签: android android-contacts android-contentresolver vcf-vcard android-7.0-nougat