简体   繁体   English

如何在不下载的情况下显示来自 GridView 中 URL 的图像?

[英]how to show images from URLs in GridView without downloading them?

Problem is that the whole process for above is very slow first, With AsyncTask class (DownloadTask), websource is downloaded.问题是上面的整个过程首先很慢,使用AsyncTask class(DownloadTask),下载websource。 Second, images' URL are downloaded and added in ArrayList (URLsList).其次,将图片的URL下载并添加到ArrayList(URLsList)中。 Third, For each URL in Arraylist images is downloaded (using ImageDownloadTask).第三,下载 Arraylist 中的每个 URL 图像(使用 ImageDownloadTask)。 Fourth, each image is added to ArrayList (ImagesList).第四,将每张图片添加到ArrayList(ImagesList)中。 Fifth, with baseadapter images are shown in gridView.第五,带有baseadapter的图像显示在gridView中。 This whole process is very slow.这整个过程非常缓慢。 How it can be made fast?怎样才能做到快速? OR how to directly pass images to Gridview without downloading it.或者如何在不下载的情况下直接将图像传递给 Gridview。 Below is the code.下面是代码。

private void GetImageList() {
// This method download image from URL Using AsyncTask
        ImageDownloadTask ImageTask;
        Bitmap Image;
        int i = 0;

        try {
            while (i < URLsList.size()) {

                ImageTask = new ImageDownloadTask();
                Image = ImageTask.execute(URLsList.get(i)).get();
                ImagesList.add(Image);
                i = i + 1;
                //Log.i("Images:" ,i + " Added");

            }

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

    }


    public void GetAllURLs() {
// This method download webSource and fetch URLs Using AsyncTask
        DownloadTask task = new DownloadTask();
        String linkToImageUrlSource = null;
        try {
            //showDialog(progress_bar_type);

            linkToImageUrlSource = task.execute("https://posh.in/kandasar").get();
            Pattern p = Pattern.compile("<a href=\"(.*?)\" class=\"image-container\"");
            Matcher m = p.matcher(linkToImageUrlSource);

            while (m.find()) {
                DownloadTask task2 = new DownloadTask();
                String imageUrlSource = null;
                try {
                    imageUrlSource = task2.execute(m.group(1)).get();
                    Pattern p1 = Pattern.compile("class=\"image-viewer-main image-viewer-container\"><img src=\"(.*?)\"");
                    Matcher m1 = p1.matcher(imageUrlSource);
                    while (m1.find()) {
                        //Log.i("URL: ", m1.group(1));
                        AdURLsList.add(m1.group(1));
                    }

                    p1 = Pattern.compile("alt=\"(.*?)\" width");
                    m1 = p1.matcher(imageUrlSource);
                    while (m1.find()) {
                        //Log.i("DESC: ", m1.group(1));
                        AdImagesDescList.add(m1.group(1));
                    }

                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }

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

    }



// BaseAdpater

    public class ImageAdapater extends BaseAdapter {
        @Override
        public int getCount() {
            return AdImagesList.size();
        }

        @Override
        public Object getItem(int i) {
            return AdImagesList.get(i);
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View galleryView = null;

            if (galleryView == null) {
                galleryView = getLayoutInflater().inflate(R.layout.layout_for_gridview, viewGroup, false);
                ImageView sampleImage = (ImageView) galleryView.findViewById(R.id.adImage);
                sampleImage.setImageBitmap(AdImagesList.get(i));
            }

            return galleryView;
        }
    }



    class DownloadTask extends AsyncTask<String, Void, String> implements com.example.urlapplication.DownloadTask {


        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();
                while (data != -1) {
                    char current = (char) data;
                    result = result + current;
                    data = reader.read();
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return "FAILED";
            }

        }

    }

    class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream inputStream = connection.getInputStream();
                Bitmap adBitmap = BitmapFactory.decodeStream(inputStream);
                return adBitmap;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

    }

How can I show images in gridview without downloading it.如何在不下载的情况下在 gridview 中显示图像。

Use Picasso/Glide to load image url into image View使用 Picasso/Glide 将图像 url 加载到图像视图中

val placeImage= itemView.findViewById<ImageView>(R.id.place_image)
Picasso.with(context).load(Uri.parse(data.url)).into(placeImage)

Check this link: https://github.com/Krishnasony/TestApp检查此链接: https://github.com/Krishnasony/TestApp

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

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