0

我正在尝试创建一个网格视图并将一些图像从 URL 加载到该网格视图,但是在加载图像时出现问题,但我找不到它,有人可以帮忙吗?

package com.images;
import java.io.InputStream;
import java.net.URL;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.coboltforge.slidemenuexample.MainActivity;
import com.coboltforge.slidemenuexample.R;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = new Integer [27];

    // Constructor
    public ImageAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /*
         * ImageView imageView = new ImageView(mContext);
         * imageView.setImageResource(mThumbIds[position]);
         * imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
         * imageView.setLayoutParams(new GridView.LayoutParams(130, 130));
         * return imageView;
         */
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        for (int i = 0; i < MainActivity.presentersImages.size(); i++) {
            imageView
                    .setImageDrawable(LoadImageFromWebOperations(MainActivity.presentersImages
                            .get(i) + position));

        }
        return imageView;
    }

    protected 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.exit(0);
            return null;
        }
    }
}

MainActivity.presentersImages 是一个字符串数组列表,包含图像的 url

4

1 回答 1

0

检查以下链接并下载对您有帮助的示例应用程序

https://github.com/nostra13/Android-Universal-Image-Loader

于 2012-08-07T13:17:05.440 回答