来自URL的图片没有显示在ImageView中,我尝试了所有我能找到的东西

时间:2011-12-24 19:49:41

标签: android url listview imageview

我有一个包含十个图像网址源的String [],我正在尝试让ImageView显示它们。我必须向每个图像显示其旁边的相应文本,即使文本显示图像也不起作用。我一直在尝试我能找到的所有解决方案,但似乎没有任何效果。我非常感谢一些指导。

这是我正在尝试的解决方案:

方法我从扩展ListActivity的类调用 (标题和图像都是String [],图像包含图像url源,标题包含图像旁边显示的文本)

setListAdapter(new MyPerformanceArrayAdapter(this, titles, imageurls));

这是MyPerformanceArrayAdapter的代码:

public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] names;
    private final String[] images;

    public MyPerformanceArrayAdapter(Activity context, String[] names, String[] images) {
        super(context, R.layout.rowlayout, names);
        this.context = context;
        this.names = names;
        this.images = images;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.rowlayout, null, true);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

        String s = names[position];
        textView.setText(s);


        Drawable drawable = LoadImageFromWebOperations(images[position]);
        imageView.setImageDrawable(drawable);

        return rowView;
    }

    private Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            System.out.println("Exc="+e);
            return null;
        }
    }
}

非常感谢!

2 个答案:

答案 0 :(得分:1)

如果您正在使用sdcard中的路径,则下面的代码将起作用,否则如果您使用drwable-res文件夹中的图像,则创建一个静态Bitmap数组,就像您为String数组所做的那样

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.rowlayout, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

    String s = names[position];
    textView.setText(s);

//在此处完成更改

    Bitmap bmp = LoadImageFromWebOperations(images[position]);
    imageView.setImageBitmap(bmp);

    return rowView;
}

//

private Drawable LoadImageFromWebOperations(String url) {
    try {
     // use bitmap instead of drawable
       Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(capturedImage));

        return bmp;
    } catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}

答案 1 :(得分:1)

为此我总是使用Bitmaps而不是drawables,到目前为止它已经完美地运作了。

尝试一下:

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View rowView = inflater.inflate(R.layout.rowlayout, null, true);
            TextView textView = (TextView) rowView.findViewById(R.id.label);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
            String s = names[position];
            textView.setText(s);
            Bitmap image = LoadImageFromWebOperations(images[position]);
            imageView.setImageBitmap(image);

            return rowView;
        }

        private Bitmap LoadImageFromWebOperations(String url) {
            try {
                URL aURL = new URL(url);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                /* Buffered is always good for a performance plus. */

                BufferedInputStream bis = new BufferedInputStream(is);
                /* Decode url-data to a bitmap. */
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
                /* Apply the Bitmap to the ImageView that will be returned. */
                return bm;

            } catch (Exception e) {
                System.out.println("Exc="+e);
                return null;
            }
        }