Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在自定义阵列适配器中加载带有联系人映像的ImageView(异步任务)_Android_Android Asynctask_Android Arrayadapter - Fatal编程技术网

Android 在自定义阵列适配器中加载带有联系人映像的ImageView(异步任务)

Android 在自定义阵列适配器中加载带有联系人映像的ImageView(异步任务),android,android-asynctask,android-arrayadapter,Android,Android Asynctask,Android Arrayadapter,我想在自定义ArrayAdapter中使用AsyncTask将图像从用户联系人加载到ImageView 这是我的图像加载代码段: class LoadImage extends AsyncTask<String, Void, Bitmap> { protected Bitmap doInBackground(String... ac) { Bitmap contactPhoto = null; try {

我想在自定义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) 
    {


    }
 }
无法加载任何内容。 我怎样才能解决这个问题?

我希望这能帮助你

class LoadImage扩展了异步任务
{
受保护列表背景(字符串…ac)
{
List totalBitmap=新建ArrayList();
位图contactPhoto=null;
尝试
{
ContentResolver cr=context.getContentResolver();
Cursor Cursor=cr.query(ContactsContract.Contacts.CONTENT\u URI,null,
“显示名称='”+ac[0]+“'”,null,null);
if(cursor.moveToFirst())
{
字符串联系人ID=
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
//获取联系人照片。
//
Uri Uri=ContentURI.withAppendedId(ContactsContract.Contacts.CONTENT\u Uri,
Long.parseLong(contactId));
输入流输入=
Contacts contract.Contacts.openContactPhotoInputStream(cr,uri);
contactPhoto=BitmapFactory.decodeStream(输入);
totalBitmap.add(contactPhoto);
}
cursor.close();
} 
捕获(例外e)
{
e、 printStackTrace();
}
返回总位图;
}
@凌驾
受保护的void onPreExecute()
{
}
@凌驾
受保护的void onPostExecute(列出contactPhoto)
{
用于(位图位图:contactPhoto){
如果(contactPhoto!=null)
{
q、 setImageBitmap(位图);
}
其他的
{
q、 setImageResource(R.drawable.no_图像);
}
}
}
}
Hai我希望这会对您有所帮助

class LoadImage扩展了异步任务
{
受保护列表背景(字符串…ac)
{
List totalBitmap=新建ArrayList();
位图contactPhoto=null;
尝试
{
ContentResolver cr=context.getContentResolver();
Cursor Cursor=cr.query(ContactsContract.Contacts.CONTENT\u URI,null,
“显示名称='”+ac[0]+“'”,null,null);
if(cursor.moveToFirst())
{
字符串联系人ID=
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
//获取联系人照片。
//
Uri Uri=ContentURI.withAppendedId(ContactsContract.Contacts.CONTENT\u Uri,
Long.parseLong(contactId));
输入流输入=
Contacts contract.Contacts.openContactPhotoInputStream(cr,uri);
contactPhoto=BitmapFactory.decodeStream(输入);
totalBitmap.add(contactPhoto);
}
cursor.close();
} 
捕获(例外e)
{
e、 printStackTrace();
}
返回总位图;
}
@凌驾
受保护的void onPreExecute()
{
}
@凌驾
受保护的void onPostExecute(列出contactPhoto)
{
用于(位图位图:contactPhoto){
如果(contactPhoto!=null)
{
q、 setImageBitmap(位图);
}
其他的
{
q、 setImageResource(R.drawable.no_图像);
}
}
}
}
            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);
            }

        }


    }
 }