简体   繁体   English

如何使用Glide [Android]下载多张图片?

[英]How to download multiple images using Glide [Android]?

Provided I have an array of image URLs, I am trying to download all these images one-by-one using glide. 如果我有一个图像URL数组,我将尝试使用glide 一张一张地下载所有这些图像。 Presently, I am able to download a single image when I provide its URL. 目前,只要提供URL,我就可以下载单个图像。 And here is the code: 这是代码:

 private void downx()
{
    File sd = getExternalCacheDir();
    File folder = new File(sd, "/mobio/");
    if (!folder.exists()) {
        if (!folder.mkdir()) {
            Log.e("ERROR", "Cannot create a directory!");
        } else {
            folder.mkdirs();
        }
    }

    final File[] fileName = {new File(folder, "one.jpg"), new File(folder, "two.jpg"),new File(folder, "three.jpg")};


    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params)
        {
            try
            {
                        theBitmap = Glide.
                        with(getApplicationContext()).
                        load(urls[2]).
                        asBitmap().
                        into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).
                        get();
            }
            catch (final ExecutionException e)
            {
                Log.e("TAG", e.getMessage());
            }
            catch (final InterruptedException e)
            {
                Log.e("TAG", e.getMessage());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void dummy) {
            if (null != theBitmap) {
                // The full bitmap should be available here
                Log.d("TAG", "Image loaded");
                Log.e("GLIDE","I am Ready");
                try {
                    FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName[1]));
                    theBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                    outputStream.close();

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



}

Now the problem is: What approach do I adopt if I need to download multiple images, and how do I force my code to adapt to handle multiple downloads? 现在的问题是:如果需要下载多个图像,我应该采用哪种方法?如何强制我的代码适应于处理多个下载?

I have used a class which takes a list of URLs and downloads the Images to a file. 我使用了一个接受URL列表并将图像下载到文件的类。 Please check this Gist for more. 请检查此要点以获取更多信息。 This uses Picasso to download Images but you can edit the download code to use glide as well. 这使用Picasso下载图像,但是您也可以编辑下载代码以使用glide。 Should be a one line change. 应该是一行更改。 Hope this helps. 希望这可以帮助。

https://gist.github.com/bpr10/a765a015bf1c774816ba58c7ae6413d6 https://gist.github.com/bpr10/a765a015bf1c774816ba58c7ae6413d6

我想最好使用Download Manager,因为它可以处理排队,网络可用性等问题。

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

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