使用Universal Image Loader加载联系人照片

时间:2015-09-01 06:24:55

标签: android android-contacts universal-image-loader

我正在加载与Uri的联系方式:

Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter))

在我尝试使用Universal Image Loader

加载联络照片ContactsContract.Contacts.PHOTO_URI之前,一切正常
ImageLoader.getInstance().displayImage(photoUri, mImageview);

我从联系人处获得的Photo Uri采用以下格式:

content://com.android.contacts/display_photo/2486

但是UIL无法加载此图片,说明此错误:

java.lang.IllegalArgumentException: URI: content://com.android.contacts/display_photo/2486/photo,
            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
            at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
            at android.content.ContentResolver.query(ContentResolver.java:478)
            at android.content.ContentResolver.query(ContentResolver.java:422)
            at android.provider.ContactsContract$Contacts.openContactPhotoInputStream(ContactsContract.java:2016)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getContactPhotoStream(BaseImageDownloader.java:232)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromContent(BaseImageDownloader.java:222)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:92)
            at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:98)
            at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:74)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:265)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:238)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:136)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

我在UIL中查找了Uri格式,但无法找到对此类Uri的支持:

Acceptable URIs examples

1 个答案:

答案 0 :(得分:0)

我认为这是一个问题, Android-Universal-Image-Loader 低于1.9.2版本,已在此处报告UIL unable to load image when source is PHOTO_URI of contact

所以根据Nostra13,如果你使用旧的通用图像加载器,你可以尝试扩展BaseImageDownloader并覆盖图像加载器类中的getStreamFromContent(...)方法:

@Override
protected InputStream getStreamFromContent(String imageUri, Object extra) throws FileNotFoundException {
        ContentResolver res = context.getContentResolver();
        Uri uri = Uri.parse(imageUri);
        if (imageUri.startsWith("content://com.android.contacts/")) {
            return ContactsContract.Contacts.openContactPhotoInputStream(res, uri);
        } else {
            return res.openInputStream(uri);
        }
    }

此问题已在Android-Universal-Image-Loader 1.9.2中修复,因此您可以download添加最新版本并在加载联系人图片时使用