【问题标题】:Get Contact List Images [Avoid OutOfMemory Exception]获取联系人列表图像 [避免 OutOfMemory 异常]
【发布时间】:2014-07-11 06:12:03
【问题描述】:

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

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

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

即在内存中获取BitMap;

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

有什么替代方法。 我想避免在内存中将图像作为位图获取。

使用 UniversalImageLoader 会有帮助???

请帮忙!

提前致谢。

【问题讨论】:

  • 你想在列表视图中显示手机联系人列表中的图像
  • 是的,但没有在内存中获取位图

标签: android bitmap uri universal-image-loader


【解决方案1】:

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

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;
    }

【讨论】:

  • 谢谢,但是这段代码获取了内存中的位图。如果联系人列表很大,这将导致 OutOfMemory。我需要使用 ContactList Photo uri 显示图像。
猜你喜欢
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2012-12-24
  • 2017-06-22
  • 1970-01-01
  • 2016-10-08
相关资源
最近更新 更多