简体   繁体   English

如何使后台线程在android,gallery小部件中加载图像

[英]How to make background thread for loading image in android ,gallery widget

I want to read images from URL and show it in android gallery widget. 我想从URL读取图像并将其显示在android gallery小部件中。

so I wrote below code in onCreate() method . 所以我在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));
        }

This is my ImageAdapter 这是我的ImageAdapter

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 = {};


    }

This throw error because it not in Thread. 抛出错误,因为它不在线程中。 How can I change this code so that URL reading and image loads in background? 如何更改此代码,以便在后台读取URL和图像?

So I have changed my ImageAdapter by using SmartImageView , which handles background thread and caching. 因此,我通过使用处理背景线程和缓存的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 = {};

    }

But my getView() is not getting called now. 但是我的getView()现在没有被调用。

you can make use of Smart Image view. 您可以使用智能图像视图。 SmartImageView is a drop-in replacement for Android's standard ImageView which additionally allows images to be loaded from URLs or the user's contact address book. SmartImageView是Android标准ImageView的直接替代,它还允许从URL或用户的联系地址簿加载图像。 Images are cached to memory and to disk for super fast loading. 图像被高速缓存到内存和磁盘,以实现超快速加载。 Please refer the following link for more info https://github.com/loopj/android-smart-image-view .hope this may help u to accomplish ur task 请参考以下链接以获取更多信息https://github.com/loopj/android-smart-image-view。希望这可以帮助您完成任务

I'd suggest writing an AsyncImageLoader class and having it handle image downloads from http. 我建议编写一个AsyncImageLoader类,并使其处理从http下载的图像。 This way you can cache and manage everything on separate threads and have it set the image to the view once the loading is complete. 这样,您可以缓存和管理单独线程上的所有内容,并在加载完成后将图像设置为视图。 Also you could use this class throughout the application if you want to download images elsewhere. 如果您想在其他地方下载图像,也可以在整个应用程序中使用此类。

you could call something like mImageLoader.loadImage(myImageView, Url) in your adapter and it would drop it in once it was finished loading. 您可以在适配器中调用mImageLoader.loadImage(myImageView, Url)类的东西mImageLoader.loadImage(myImageView, Url)并在完成加载后将其放入。

if you want more details let me know :) 如果您想了解更多详细信息,请告诉我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM