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

  • SEARCH
  • Home
  • 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 7996781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:47:28+00:00 2026-06-04T14:47:28+00:00

I have modified the code in this question ,according to the answers there, in

  • 0

I have modified the code in this question ,according to the answers there, in order to load contact’s picture on a ListView. I am getting the Photo_id, and use it to get the Bitmap of the contact, using loadContactPhoto(ContentResolver cr, long id) . The problem is that no ImageView is getting a new image, although the photo id is always different. I tried using the Contact._ID, but still only two contacts’ ImageView got a contact picture, and they were both wrong. I have commented the new lines I have added below.

Here is the code after the edit:

ContactStock:

public class ContactStock {

private String name;
private String number;
private Bitmap picture;


public ContactStock(String name, String number) {
    this.name = name;
    this.number = number;
}

public ContactStock(String name, String number, Bitmap photo) {
    this.name = name;
    this.number = number;
    this.picture = photo;
}

public void setName(String name) {
    this.name = name;
}

public void setNumber(String number) {
    this.number = number;
}

public String getName() {
    return this.name;
}

public String getNumber() {
    return this.number;
}

public void setPicture(Bitmap picture) { // NEW METHOD
    this.picture = picture;
}

public Bitmap getPicture() { // NEW METHOD
    return picture;
}
}

addlistfromcontact:

    public class addlistfromcontact extends Activity {
private ListView lst;
private List<ContactStock> contactstock;
private Cursor mCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_contact_list);
    lst = (ListView) findViewById(R.id.tab_contact_list);
    contactstock = new ArrayList<ContactStock>();

    mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null,
            Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null,
            ContactsContract.Data.DISPLAY_NAME + " ASC");

    int number = mCursor.getColumnIndex(Phone.NUMBER);
    int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
    int id = mCursor.getColumnIndex(Contacts.PHOTO_ID); // NEW LINE

    while (mCursor.moveToNext()) {

        String phName = mCursor.getString(name);
        String phNumber = mCursor.getString(number);
        long phId = mCursor.getLong(id); // NEW LINE

        Bitmap phPhoto = loadContactPhoto(getContentResolver(), phId); // NEW LINE
        Log.d("phId=", phId + "");

        contactstock.add(new ContactStock(phName, phNumber, phPhoto)); // NEW LINE EDIT
    }
    lst.setAdapter(new ContactListAdapter(addlistfromcontact.this,
            contactstock));
}

public static Bitmap loadContactPhoto(ContentResolver cr, long id) { // NEW METHOD
    Uri uri = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts
            .openContactPhotoInputStream(cr, uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

    }

ContactListAdapter:

public class ContactListAdapter extends ArrayAdapter {
    private final Activity activity;
    private final List stocks;

    public ContactListAdapter(Activity activity, List objects) {
        super(activity, R.layout.listview_detail_tab_contact_list, objects);
        this.activity = activity;
        this.stocks = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail_tab_contact_list, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.contact_name);
            sv.number = (TextView) rowView.findViewById(R.id.contact_number);
            sv.photo = (ImageView) rowView.findViewById(R.id.contact_photo);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());
        sv.photo.setImageBitmap(currentStock.getPicture()); // NEW LINE

        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
        protected ImageView photo; // NEW LINE
    }
}
  • 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-06-04T14:47:30+00:00Added an answer on June 4, 2026 at 2:47 pm

    There are two problems in your code. The first one is easy to overcome, the others need more work.

    Lets start with the easy one:
    The method ContactsContract.Contacts.openContactPhotoInputStream(cr, uri) takes a contact uri and not a photo uri. That is the id in your call to ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id) has to be a contact id.

    Also you create a lot of Bitmap objects when iterating over the result set. Don’t do this. Though this might work at first, it probably crashes with OutOfMemory errors when the list get’s to long. Try to create as few Bitmapobjects as necessary. That is: Only for those rows visible. When scrolling the list view you have to recycle existing Bitmaps.

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

Sidebar

Related Questions

I have code that looks like this: itemView.Question.AnswersJSON = itemView.Answer.ToJSONString(); itemView.Question.Modified = DateTime.Now; itemView.Question.ModifiedBy
While implementing the code from this question on my project I realized there's 3
Recently I have modified my code to While taking input form STDIN, I moved
I have the MySQL Connector/NET installed on my PC. I modified the source code
This is a question based on C code for Win32. In one of my
In what order get the constructors of global object in C++ called? This question
Alternate question title would be: How to explicitly have the compiler generate code for
Description of the problem: I have a JFrame, inside this JFrame there is a
I modified this stack question: Applying watermarks on pdf files when users try to
I have modified the Nerd Dinner application to allow editing of child records by

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.