我想使用自定义ArrayAdapter中的AsyncTask将用户联系人中的图像加载到ImageView中

这是我的图像加载代码段:

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)
    {


    }
 }


在此代码中,图像加载到错误的位置,例如,为人1设置的imgae为人3设置的!

如果我将此代码放在onPostExecute方法中:

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


无法加载任何内容。
我该如何解决这个问题?

最佳答案

海,希望这对您有帮助

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

        }


    }
 }

10-07 19:20
查看更多