【问题标题】:How to download multiple images using Glide [Android]?如何使用 Glide [Android] 下载多张图片?
【发布时间】:2017-01-12 05:08:58
【问题描述】:

如果我有一组图像 URL,我正在尝试使用 glide 下载所有这些图像一张一张。目前,当我提供其 URL 时,我可以下载单个图像。这是代码:

 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();



}

现在的问题是:如果我需要下载多个图像,我应该采用什么方法,以及如何强制我的代码适应处理多个下载?

【问题讨论】:

    标签: java android image android-image android-glide


    【解决方案1】:

    我使用了一个类,它获取 URL 列表并将图像下载到文件中。请查看此Gist 了解更多信息。这使用 Picasso 下载图像,但您也可以编辑下载代码以使用 glide。应该是一行更改。希望这可以帮助。

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

    【讨论】:

      【解决方案2】:

      我想使用下载管理器会更好,因为它可以处理排队、网络可用性等问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多