user1624220
user1624220

Reputation: 25

Load ImageView in Custom ArrayAdapter with Contact Image (AsyncTask)

I want to load images from user contacts to an ImageView with AsyncTask in a custom ArrayAdapter

This is my code snippet for image loading:

class LoadImage extends AsyncTask<String, Void, Bitmap> 
{

    protected Bitmap doInBackground(String... ac) 
    {
        Bitmap contactPhoto = null;
        try 
        {
           ContentResolver cr = context.getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            "DISPLAY_NAME = '" + ac[0] + "'", null, null);
            if (cursor.moveToFirst()) 
            {
                String contactId =
                    cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                //
                // Get the contact photo.
                //
                Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                    Long.parseLong(contactId));
                InputStream input =
                    ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
                contactPhoto = BitmapFactory.decodeStream(input);
            }
            cursor.close();
            if(contactPhoto != null)
            {
                q.setImageBitmap(contactPhoto);
            }
            else
            {
               q.setImageResource(R.drawable.no_image);
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return contactPhoto;
    }

    @Override
    protected void onPreExecute()
    {

    }

    @Override
    protected void onPostExecute(Bitmap contactPhoto) 
    {


    }
 }

In this code, Images load in wrong place, for example, imgae for person 1 set for person 3!

If i put this code in onPostExecute method:

            if(contactPhoto != null)
            {
                q.setImageBitmap(contactPhoto);
            }
            else
            {
               q.setImageResource(R.drawable.no_image);
            }

Nothing could not be loaded. How i can fix this problem?

Upvotes: 1

Views: 682

Answers (1)

Bebin T.N
Bebin T.N

Reputation: 2649

Hai I hope this will help you

    class LoadImage extends AsyncTask<String, Void, List<Bitmap>> 
{

    protected List<Bitmap> doInBackground(String... ac) 
    {
        List<Bitmap> totalBitmap = new ArrayList<Bitmap>();
        Bitmap contactPhoto = null;
        try 
        {
           ContentResolver cr = context.getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            "DISPLAY_NAME = '" + ac[0] + "'", null, null);
            if (cursor.moveToFirst()) 
            {
                String contactId =
                    cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                //
                // Get the contact photo.
                //
                Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                    Long.parseLong(contactId));
                InputStream input =
                    ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
                contactPhoto = BitmapFactory.decodeStream(input);
                totalBitmap.add(contactPhoto);
            }
            cursor.close();

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return totalBitmap;
    }

    @Override
    protected void onPreExecute()
    {

    }

    @Override
    protected void onPostExecute(List<Bitmap> contactPhoto) 
    {
        for (Bitmap bitmap : contactPhoto) {

            if(contactPhoto != null)
            {
                q.setImageBitmap(bitmap);
            }
            else
            {
               q.setImageResource(R.drawable.no_image);
            }

        }


    }
 }

Upvotes: 1

Related Questions