获取联系人列表图像[避免OutOfMemory例外]

时间:2014-07-11 06:12:03

标签: android bitmap uri universal-image-loader

我需要在我的应用程序中的listView中显示所有联系人:

但是在使用以下代码显示联系人图像时:

viewholder.imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(activity
                    .getContentResolver(), Uri.parse(currentItem.getContactImage())));

将BitMap放入内存中;

当我滚动列表视图时,GC被多次调用,在这种情况下我担心OutOfMemory异常。

对此有什么替代方法。 我想避免将图像作为BitMap存储在内存中。

使用UniversalImageLoader将有助于???

请帮忙!

提前致谢。

1 个答案:

答案 0 :(得分:0)

我很久以前从堆栈中获取此代码无法找到源代码但有助于从联系人列表中获取图像只是将电话号码作为参数

public Bitmap getFacebookPhoto(String phoneNumber) {
        Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Uri photoUri = null;
        ContentResolver cr = _context.getContentResolver();
        Cursor contact = cr.query(phoneUri,
                new String[] { ContactsContract.Contacts._ID }, null, null, null);

        if (contact.moveToFirst()) {
            long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID));
            photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);

        }
        else {
            Bitmap defaultPhoto = BitmapFactory.decodeResource(_context.getResources(), android.R.drawable.ic_menu_report_image);
            return defaultPhoto;
        }
        if (photoUri != null) {
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                    cr, photoUri);
            if (input != null) {
                return BitmapFactory.decodeStream(input);
            }
        } else {
            Bitmap defaultPhoto = BitmapFactory.decodeResource(_context.getResources(), android.R.drawable.ic_menu_report_image);
            return defaultPhoto;
        }
        Bitmap defaultPhoto = BitmapFactory.decodeResource(_context.getResources(), android.R.drawable.ic_menu_report_image);
        return defaultPhoto;
    }