如何从 json 文件中的链接加载图像到列表视图单元格中的图像视图

how to load images from links in json file to image view in listview cells

我试图在列表视图中显示来自 json 文件中链接的图像以及文本视图,然后以圆形显示它们。但是文本视图显示但不显示图像。这是我为图像尝试的代码片段;

public class ChatsArrayAdapter extends ArrayAdapter<ChatData>
{
ImageLoader imageLoader;

public ChatsArrayAdapter(Context context, List<ChatData> objects)
{
    super(context, 0, objects);
    //imageLoader=new ImageLoader(imageviewfromchat, avatar_url);
}

public View getView(int position, View convertView, ViewGroup parent)
{
    ChatCell chatCell = new ChatCell();
    ImageLoader imageLoader=new ImageLoader(getContext());

    LayoutInflater inflater = LayoutInflater.from(getContext());
    convertView = inflater.inflate(R.layout.cell_chat, parent, false);

    chatCell.imageviewfromchat=(ImageView)convertView.findViewById(R.id.imageviewfromchat);

    ChatData chatData = getItem(position);

    imageLoader.DisplayImage(chatData.avatarURL,chatCell.imageviewfromchat);
    //imageLoader.getBitmap(chatData.avatarURL,chatCell.imageviewfromchat);

    return convertView;
}

private static class ChatCell
{
    ImageView imageviewfromchat;
}

private class ImageLoader {
    private Map<ImageView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService;
    // Handler to display images in UI thread


    public ImageLoader(Context context) {
        executorService = Executors.newFixedThreadPool(5);
    }

    public void DisplayImage(String url, ImageView imageView) {
        imageViews.put(imageView, url);
        getBitmap(url);
        //getRoundedShape(bitmap)
    }

    private void queuePhoto(String url, ImageView imageView) {
        PhotoToLoad p = new PhotoToLoad(url, imageView);

    }

    private Bitmap getBitmap(String url) {
        // Download Images from the Internet
        Bitmap bitmap = null;
        try {
            bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            OutputStream os = conn.getOutputStream();
            Utils.CopyStream(is, os);
            os.close();
            conn.disconnect();
            return bitmap;
        } catch (Throwable ex) {
            ex.printStackTrace();
            if (ex instanceof OutOfMemoryError)

                return null;
        }
        return bitmap;
    }

    public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
        int targetWidth = 50;
        int targetHeight = 50;
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
                targetHeight,Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth),
                        ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);
        Bitmap sourceBitmap = scaleBitmapImage;
        canvas.drawBitmap(sourceBitmap,
                new Rect(0, 0, sourceBitmap.getWidth(),
                        sourceBitmap.getHeight()),
                new Rect(0, 0, targetWidth, targetHeight), null);
        return targetBitmap;
    }

    // Task for the queue
    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }
}

private static class Utils {
    public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
                int count=is.read(bytes, 0, buffer_size);
                if(count==-1)
                    break;
                os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
}
}

我相信我的代码是正确的,但我也可能是错的。有人可以帮忙吗? 这不是网络线程异常重复,我只是想在我的列表视图 SELVIN 中显示图像

使用 Picasso 加载图片

Picasso.with(context).load(imgUrl)
                .placeholder(R.drawable.img_placeholder)
                .error(R.drawable.error_img)
                .into(holder.desire_img);

作为参考,您可以使用 link here