Android 如何将json文件中的链接中的图像加载到listview单元格中的图像视图

Android 如何将json文件中的链接中的图像加载到listview单元格中的图像视图,android,json,listview,getjson,universal-image-loader,Android,Json,Listview,Getjson,Universal Image Loader,我试图在列表视图中显示来自json文件中链接的图像以及文本视图,然后以圆形显示它们。但是,文本视图显示的是图像,而不是图像。这是一个代码片段,用于我尝试的图像 public class ChatsArrayAdapter extends ArrayAdapter<ChatData> { ImageLoader imageLoader; public ChatsArrayAdapter(Context context, List<ChatData> objects) {

我试图在列表视图中显示来自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){}
    }
}
}
公共类ChatsArrayAdapter扩展了ArrayAdapter
{
图像加载器;
公共聊天室阵列适配器(上下文、列表对象)
{
超级(上下文,0,对象);
//imageLoader=新的imageLoader(imageviewfromchat,avatar_url);
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
ChatCell ChatCell=新ChatCell();
ImageLoader ImageLoader=新的ImageLoader(getContext());
LayoutInflater充气器=LayoutInflater.from(getContext());
convertView=充气机。充气(R.layout.cell\u chat,父项,false);
chatCell.imageviewfromchat=(ImageView)convertView.findViewById(R.id.imageviewfromchat);
ChatData ChatData=getItem(位置);
imageLoader.DisplayImage(chatData.avatarURL、chatCell.imageviewfromchat);
//imageLoader.getBitmap(chatData.avatarURL、chatCell.imageviewfromchat);
返回视图;
}
私有静态类聊天室
{
ImageView imageviewfromchat;
}
私有类ImageLoader{
私有地图图像视图=集合
.synchronizedMap(新WeakHashMap());
执行服务执行服务;
//在UI线程中显示图像的处理程序
公共图像加载器(上下文){
executorService=Executors.newFixedThreadPool(5);
}
public void DisplayImage(字符串url,ImageView){
put(imageView,url);
获取位图(url);
//getRoundedShape(位图)
}
私有void队列照片(字符串url,ImageView){
PhotoToLoad p=新的PhotoToLoad(url,imageView);
}
私有位图getBitmap(字符串url){
//从Internet下载图像
位图=空;
试一试{
位图=空;
URL imageUrl=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)imageUrl
.openConnection();
连接设置连接超时(30000);
连接设置读取超时(30000);
conn.setInstanceFollowRedirects(真);
InputStream is=conn.getInputStream();
OutputStream os=conn.getOutputStream();
Utils.CopyStream(is,os);
os.close();
连接断开();
返回位图;
}捕获(可丢弃的ex){
例如printStackTrace();
if(ex instanceof OutOfMemoryError)
返回null;
}
返回位图;
}
公共位图getRoundedShape(位图缩放图像){
int targetWidth=50;
int targetHeight=50;
位图targetBitmap=Bitmap.createBitmap(targetWidth,
targetLight,Bitmap.Config.ARGB_8888);
画布画布=新画布(targetBitmap);
路径路径=新路径();
path.addCircle(((float)targetWidth-1)/2,
((浮动)目标光-1)/2,
(数学最小值((浮点)目标宽度),
((浮动)目标光))/2),
路径.方向.逆时针方向);
canvas.clipPath(路径);
位图源位图=缩放比例位图;
canvas.drawBitmap(sourceBitmap,
新的Rect(0,0,sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
新的Rect(0,0,targetWidth,targetLight),null;
返回target位图;
}
//队列的任务
私有类光电负载{
公共字符串url;
公共影像视图;
公共PhotoToLoad(字符串u,图像视图i){
url=u;
imageView=i;
}
}
}
私有静态类Utils{
公共静态void CopyStream(InputStream是,OutputStream os)
{
最终整数缓冲区大小=1024;
尝试
{
字节[]字节=新字节[缓冲区大小];
对于(;;)
{
int count=is.read(字节,0,缓冲区大小);
如果(计数==-1)
打破
写入(字节,0,计数);
}
}
捕获(例外情况除外){}
}
}
}
我相信我的代码是正确的,但我可能是错的。有人能帮忙吗?
这不是网络阅读异常重复,我只是想在我的LISTVIEW中显示图像。请使用毕加索加载图像

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

作为参考,您可以使用link

@Selvin这是我第一次使用json,对不起。我到底在哪里用它?@Selvin你不觉得我查过了吗?如果您不能帮助我,请退出您的
getBitmap
方法,该方法似乎总是返回null。@Nija我明白您的意思,谢谢。我编辑了这个问题。图像仍然不加载no;你不明白我的意思。编辑对返回的内容没有任何影响。