Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:02:52+00:00 2026-05-31T06:02:52+00:00

I use the following function to retrieve a contact’s photo in Android where the

  • 0

I use the following function to retrieve a contact’s photo in Android where the person’s lookup key is given:

public Bitmap getContactPhoto(String lookup_key) {
    Uri lookUpUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup_key);
    Uri contentUri = ContactsContract.Contacts.lookupContact(ctx.getContentResolver(), lookUpUri);
    InputStream stream = null;
    try {
        stream = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), contentUri);
    }
    catch (Exception e) {
    }
    if (stream != null) {
        return BitmapFactory.decodeStream(stream, null, bitmapOptions);
    }
    else {
        return null;
    }
}

When I display those contact photos in a list view, I sometimes read the following error in the developer console’s crash reports:

Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6023KB, Allocated=3002KB, Bitmap Size=27152KB)

Does this mean that my app may use about 3MB of heap but a single bitmap had roughly 27MB?

I read a lot of questions here on Stack Overflow concerning OutOfMemory error but it was mainly about:

  • leaking contexts so that the garbage collector cannot free the resources
  • huge bitmaps that need to be scaled

But how can I prevent the error in my case? Since I only get the contacts’ photos, I don’t know if I have huge bitmaps that must be scaled. And leaking a context doesn’t seem to be the case here.

This is how the images are displayed:

imageView.setVisibility(View.VISIBLE);
imageView.setBackgroundResource(R.drawable.background);
if (<BITMAP_OBJECT> != null) {
    imageView.setImageBitmap(<BITMAP_OBJECT>);
}
else {
    imageView.setImageBitmap(null);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:02:53+00:00Added an answer on May 31, 2026 at 6:02 am

    When you are setting your Bitmap to an ImageView, try doing this:

    Bitmap oldBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    imageView.setImageDrawable(null);
    oldBitmap.recycle();
    imageView.setImageBitmap(newBitmap);
    

    It may not complete get rid of the problem, but it certainly makes it happen much more rarely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following function to show controls on my form: class procedure TFormMain.FadeControls(ctrl:Array
I use the following function to rename thumbnails. For example, if I upload a
We use the following function to auto detect if we are on a machine
i have to use the following function, to change the input's type <input class=input
I use the following javascript function, function get_check_value(formId,checkboxId,TxtboxId) { alert(formId); var c_value = ;
I would like to know if I can use the following JQuery function on
I use the following jquery statements but i get error in this function onGetDataSuccess(result)
I use the following the jquery statements to call my php controller function, it
I use a function that sends emails to some users. I use the following
I use the following jquery statements, $(.resultsdiv:odd).css(background-color, #fff); $(.resultsdiv:even).css(background-color, #EFF1F1); $('.resultsdiv').hover(function() { $(this).css('background-color', '#f4f2f2');

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.