gpt4 book ai didi

android - 使用联系人图像 (AsyncTask) 在自定义 ArrayAdapter 中加载 ImageView

转载 作者:行者123 更新时间:2023-11-30 03:41:48 25 4
gpt4 key购买 nike

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

这是我的图片加载代码 fragment :

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 的图像设置为人 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);
}

}


}
}

关于android - 使用联系人图像 (AsyncTask) 在自定义 ArrayAdapter 中加载 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15598168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com