列表视图不显示远程图像

发布于 2024-12-04 16:21:19 字数 799 浏览 0 评论 0原文

我正在使用 HashMap 通过 simpleAdapter 在列表视图中显示图像和文本。现在,这可以很好地处理我的 R.drawable 中的图像。但是,一旦我从远程源以位图形式检索图像,它就不会显示该图像。图像正在正确下载,并且当我在图像视图上显示它时,它显示得很好。这是我用于检索图像并将其存储在位图变量中的代码:

   user_picture=(ImageView)findViewById(R.id.widget89);
    java.net.URL img_value = null;
    try {
        img_value = new java.net.URL("http://graph.facebook.com/XXXXXXX/picture?type=small");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    user_picture.setImageBitmap(mIcon1);

有什么想法为什么会发生这种情况吗?

提前致谢 詹尼克

I am using a HashMap to display images and text in a listview via a simpleAdapter. Now this works fine with images from my R.drawable. But as soon as I retrieve the image as a bitmap from a remote source, it doesn't display the image. The image is being downloaded correctly and it displays fine when I display it on a image view. Here is my code for retrieving an image and storing it in a bitmap variable:

   user_picture=(ImageView)findViewById(R.id.widget89);
    java.net.URL img_value = null;
    try {
        img_value = new java.net.URL("http://graph.facebook.com/XXXXXXX/picture?type=small");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    user_picture.setImageBitmap(mIcon1);

Any ideas why this is happening?

Thanks in advance
Jannik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-12-11 16:21:19

在最近的项目中,我使用下面的类来获取位图并将它们存储在自定义对象中,并使用它们使用适配器填充 ListView:

package com.octoshape.android.octopocplayer.playlist;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class BitmapDownloader extends AsyncTask<String, Void, Bitmap>{

    @Override
    protected Bitmap doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]); 
            URLConnection conn = url.openConnection();
            conn.connect();
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            return bm;
        } catch (IOException e) {}
            return null;
    }
}

这是适配器:

private class VideoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return playList.size();
        }

        @Override
        public Object getItem(int position) {
            return playList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return ITEM_VIEW_TYPE_COUNT;
        }

        @Override
        public int getItemViewType(int position) {
            return (playList.get(position) instanceof String) ? ITEM_VIEW_TYPE_CHANNELSET
                    : ITEM_VIEW_TYPE_CHANNEL;
        }

        @Override
        public boolean isEnabled(int position) {
            // A separator cannot be clicked !
            return getItemViewType(position) != ITEM_VIEW_TYPE_CHANNELSET;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final int type = getItemViewType(position);

            // First, let's create a new convertView if needed. You can also
            // create a ViewHolder to speed up changes if you want ;)
            if (convertView == null) {
                final LayoutInflater inflater = LayoutInflater
                        .from(DemoPlayer.this);
                final int layoutID = type == ITEM_VIEW_TYPE_CHANNELSET ? R.layout.separator_list_item
                        : R.layout.video_list_item;
                convertView = inflater.inflate(layoutID, parent, false);
            }

            // We can now fill the list item view with the appropriate data.
            if (type == ITEM_VIEW_TYPE_CHANNELSET) {
                ((TextView) convertView).setText((String) getItem(position));
            } else {
                final Channel channel = (Channel) getItem(position);
                ((TextView) convertView.findViewById(R.id.name))
                        .setText(channel.getName());
                ((ImageView) convertView.findViewById(R.id.logo))
                        .setImageResource(R.drawable.default);

                if (channel.getChannelImage() != null)
                    ((ImageView) convertView.findViewById(R.id.channellogo))
                            .setImageBitmap(channel.getChannelImage());
            }
            return convertView;
        }
    }

In recent projects, I have used the below class for fetching bitmaps and storing these in custom objects and using these for populating a ListView using an Adapter:

package com.octoshape.android.octopocplayer.playlist;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class BitmapDownloader extends AsyncTask<String, Void, Bitmap>{

    @Override
    protected Bitmap doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]); 
            URLConnection conn = url.openConnection();
            conn.connect();
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            return bm;
        } catch (IOException e) {}
            return null;
    }
}

Here is the Adapter:

private class VideoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return playList.size();
        }

        @Override
        public Object getItem(int position) {
            return playList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return ITEM_VIEW_TYPE_COUNT;
        }

        @Override
        public int getItemViewType(int position) {
            return (playList.get(position) instanceof String) ? ITEM_VIEW_TYPE_CHANNELSET
                    : ITEM_VIEW_TYPE_CHANNEL;
        }

        @Override
        public boolean isEnabled(int position) {
            // A separator cannot be clicked !
            return getItemViewType(position) != ITEM_VIEW_TYPE_CHANNELSET;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final int type = getItemViewType(position);

            // First, let's create a new convertView if needed. You can also
            // create a ViewHolder to speed up changes if you want ;)
            if (convertView == null) {
                final LayoutInflater inflater = LayoutInflater
                        .from(DemoPlayer.this);
                final int layoutID = type == ITEM_VIEW_TYPE_CHANNELSET ? R.layout.separator_list_item
                        : R.layout.video_list_item;
                convertView = inflater.inflate(layoutID, parent, false);
            }

            // We can now fill the list item view with the appropriate data.
            if (type == ITEM_VIEW_TYPE_CHANNELSET) {
                ((TextView) convertView).setText((String) getItem(position));
            } else {
                final Channel channel = (Channel) getItem(position);
                ((TextView) convertView.findViewById(R.id.name))
                        .setText(channel.getName());
                ((ImageView) convertView.findViewById(R.id.logo))
                        .setImageResource(R.drawable.default);

                if (channel.getChannelImage() != null)
                    ((ImageView) convertView.findViewById(R.id.channellogo))
                            .setImageBitmap(channel.getChannelImage());
            }
            return convertView;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文