【问题标题】:Best way to download image from URL and save it into internal storage memory从 URL 下载图像并将其保存到内部存储器的最佳方法
【发布时间】:2015-04-14 11:00:20
【问题描述】:

我正在开发一个应用程序,我想在其中从 URL 下载图像。我需要立即下载这些图像并将其存储到内部存储中。有200多张图片可供下载。请告诉我在尽可能短的时间内下载这些图像的最佳方式。如果有任何第三方库可用,请告知。

【问题讨论】:

  • 你可以使用 Picasso 或 Volley
  • 毕加索不会将这些图像保存到内部存储中。它直接显示在ui上
  • 您可以使用 Volley lib 并创建一个自定义请求,在成功响应时将您的图像直接写入设备内存。
  • 你可能想看看我的这篇帖子stackoverflow.com/questions/15549421/…
  • 为此使用 AQuery。它将图像存储在内存缓存和本地存储中。检查:code.google.com/p/android-query/wiki/ImageLoading

标签: android


【解决方案1】:

考虑将毕加索用于您的目的。我在我的一个项目中使用它。要将图像保存在外部磁盘上,您可以使用以下方法:

 Picasso.with(mContext)
        .load(ImageUrl)
        .into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                try {
                    String root = Environment.getExternalStorageDirectory().toString();
                    File myDir = new File(root + "/yourDirectory");

                    if (!myDir.exists()) {
                        myDir.mkdirs();
                    }

                    String name = new Date().toString() + ".jpg";
                    myDir = new File(myDir, name);
                    FileOutputStream out = new FileOutputStream(myDir);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                    out.flush();
                    out.close();                        
                } catch(Exception e){
                    // some action
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }
        }
    );

From here你可以下载这个库。

【讨论】:

  • 我想下载 200 张图片。为此我需要循环下载这些图像
  • @astuter 你能解释一下保存后如何检索图像吗?
  • @Signo:查看 Dhawal Shoda parmar 的答案,在此链接中:stackoverflow.com/a/15918369/2571277
  • @astuter 非常感谢您的快速回复! :) 我试过但没有运气,它说如果 ExternalStorage 什么都没有,现在我会做一些调试以找出问题所在,再次感谢 :)
  • 谢谢!我是 android 新手,这很有帮助。
【解决方案2】:

您可以从这样的网址下载图片:

URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
   out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

然后您可能想要保存图像,这样做:

FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
fos.write(response);
fos.close();

【讨论】:

    【解决方案3】:
    BitmapDrawable bitmapDrawable = (BitmapDrawable) holder.post_image.getDrawable();
                            Bitmap bitmap = bitmapDrawable.getBitmap();
                            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                            MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bitmap, "IMG_" + Calendar.getInstance().getTime(), null);
    

    这是一个相当简单的工作......

    【讨论】:

      【解决方案4】:
       const actionDownloadImage = (urls) => {
              urls.map((url) => {
                  const splitUrl = url.split("/");
                  const filename = splitUrl[splitUrl.length - 1];
                  fetch(url)
                      .then((response) => {
                          response.arrayBuffer().then(function (buffer) {
                              const url = window.URL.createObjectURL(new Blob([buffer]));
                              const link = document.createElement("a");
                              link.href = url;
                              link.setAttribute("download", filename); //or any other extension
                              document.body.appendChild(link);
                              link.click();
                              document.body.removeChild(link);
                          });
                      })
                      .catch((err) => {
                          console.log(err);
                      });
              });
          }
      

      【讨论】:

        猜你喜欢
        • 2020-05-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-06
        • 2020-06-30
        • 1970-01-01
        • 2017-11-03
        • 2019-05-26
        • 1970-01-01
        相关资源
        最近更新 更多