0

我想从 URL 读取图像并将其显示在 android 图库小部件中。

所以我在 onCreate() 方法中写了下面的代码。

        list = GuideDAO.getAllImages(businessId);


        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setSpacing(2);

        // Set the adapter to our custom adapter (below)
        if(list.size() > 0)
        {
            g.setAdapter(new ImageAdapter(this,list));
        }

这是我的图像适配器

public class ImageAdapter extends BaseAdapter {
       List<Images> glist = null;
       private String url;
        public ImageAdapter(Context c,List<Images> lst) {
            mContext = c;
            glist = lst;
            int i=0;
            for (Images id : glist) {
                 url = id.getImageURL(); // Getting URL
                 InputStream inStream = null;
                    if (url.startsWith("http")) {

                        url = url.replace(" ", "%20");
                        HttpURLConnection conn;
                        try {
                             conn = (HttpURLConnection)new URL(url).openConnection();
                             conn.setDoInput(true);
                             conn.connect();
                             inStream = conn.getInputStream();
                        } catch (MalformedURLException e) {

                            e.printStackTrace();
                        } catch (IOException e) {

                            e.printStackTrace();
                        }

                    } else {
                        try {
                            inStream = new FileInputStream(url);
                        } catch (FileNotFoundException e) {

                            e.printStackTrace();
                        }
                    }

                     BitmapFactory.Options options = new BitmapFactory.Options();
                     options.inPreferredConfig = Bitmap.Config.RGB_565;
                     options.inPurgeable = true;
                     Bitmap b = BitmapFactory.decodeStream(inStream, null, options);

                     mImageCollection[i]=b;


                i++;
            }
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

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

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

            ImageView i = new ImageView(mContext);

            i.setImageBitmap(mImageCollection[position]);
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return i;
        }

        private Context mContext;
        private String[] mImageURLs = {};
        private Bitmap[] mImageCollection = {};


    }

这抛出错误,因为它不在线程中。如何更改此代码以便在后台加载 URL 读取和图像?

所以我通过使用处理后台线程和缓存的 SmartImageView 更改了我的 ImageAdapter。

public class ImageAdapter extends BaseAdapter {
       List<ImageGallery> glist = null;
       private String url;
        public ImageAdapter(Context c,List<ImageGallery> lst) {
            mContext = c;
            glist = lst;
            int i=0;
            al = new ArrayList<String>(); 
            for (ImageGallery id : glist) {

                al.add(id.getImageURL());

            }   
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
           Log.d("deepak", "getview gallery");
            SmartImageView i = new SmartImageView(mContext);
            i.setImageUrl(al.get(position));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return i;
        }

        private Context mContext;
        private String[] mImageURLs = {};
        private ArrayList<String> al; 
        private Bitmap[] mImageCollection = {};
        private Integer[] mImageIds = {};

    }

但是我的 getView() 现在没有被调用。

4

2 回答 2

0

您可以使用智能图像视图。SmartImageView 是 Android 标准 ImageView 的替代品,它还允许从 URL 或用户的联系人地址簿加载图像。图像缓存到内存和磁盘以实现超快速加载。请参阅以下链接以获取更多信息https://github.com/loopj/android-smart-image-view。希望这可以帮助您完成您的任务

于 2012-11-30T17:40:17.800 回答
0

我建议编写一个 AsyncImageLoader 类并让它处理从 http 下载的图像。这样,您可以在单独的线程上缓存和管理所有内容,并在加载完成后将图像设置为视图。如果你想在其他地方下载图像,你也可以在整个应用程序中使用这个类。

你可以mImageLoader.loadImage(myImageView, Url)在你的适配器中调用类似的东西,一旦完成加载它就会把它放进去。

如果您想了解更多详细信息,请告诉我:)

于 2012-11-30T18:30:04.163 回答