简体   繁体   中英

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.

so I wrote below code in onCreate() method .

        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

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?

So I have changed my ImageAdapter by using SmartImageView , which handles background thread and caching.

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.

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. 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

I'd suggest writing an AsyncImageLoader class and having it handle image downloads from 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.

if you want more details let me know :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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