检索URL(Internet)的所有图像并保存在本地(SD卡) - 机器人 [英] Retrieve ALL images from URL (internet) and save them locally (sdcard) - Android

查看:124
本文介绍了检索URL(Internet)的所有图像并保存在本地(SD卡) - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯......我的应用程序在互联网上的图像的地段。它们存储在文件夹和子文件夹类似类别。

Well... my application has LOTS of images in the internet. They are stored in folders and subfolders like categories.

当我的应用程序的第一个开始,我希望允许用户下载所有这些内容对他/她的内部SD卡。

Upon the first start of my app, I would like to allow the user to download all this content to his/her internal sdcard.

下面是我想要做什么:

    URL url = new URL ("www.mysite.com/folder1/subfolder1/anImage.png");
    InputStream input = url.openStream();
    try {
        OutputStream output = new FileOutputStream ("/sdcard/myImage.png");
        try {
            byte[] buffer = new byte[aReasonableSize];
            int bytesRead = 0;
            while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                output.write(buffer, 0, bytesRead);
            }
        } finally {
            output.close();
        }
    } finally {
        input.close();
    }

好吧......在code以上,让我救一个图像从一个链接。

Okay... the code above allows me to save ONE image from one link.

我怎样才能找回它们呢?

此外,有可能通知用户有多少数据(MB)将被转移?

Also, is it possible inform the user how many data (MB) will be transfered?

任何帮助是AP preciatted!

Any help is appreciatted!

谢谢!

推荐答案

这里是在怎样一个很好的例子和说明下载图像
您可以通过下面的方法计算的远程文件的大小。通过网址数组这个方法,它会回报你以字节为单位的大小。

Here is a good example and explanation on how to download images.
You can calculate the size of remote files by following method. Pass array of urls to this method and it will return you the size in bytes.

public static int getSize(String[] urls){
    int sizeInBytes = 0;
    for (String string : urls) {
        sizeInBytes += ((int) (new File(string)).length());         
    }
    return sizeInBytes;
}


你也可以计算出图像的大小一旦被下载到SD卡传递的目录文件(其中已保存的所有图像),以的getSize(...)方法,它会返回目录的大小以字节为单位。


You can also calculate the size of images once they are downloaded on sd-card Pass the directory file (where you have saved all images) to getSize(...) method and it will return the size of directory in bytes.

String basePath = Environment.getExternalStorageDirectory().toString();
File file = new File(basePath +"file/myImageDirectory/");
ClassName.getSize(file);

方法来计算目录的大小。

Method to calculate size of Directory.

public static int getSize(File directory) {
    return getSize(directory, 0);
}
private static int getSize(File directory,int sizeInBytes) {

    if (directory == null)
        return sizeInBytes;
    if (!directory.exists())
        return sizeInBytes;
    if (directory.isDirectory()) {
        String[] list = directory.list();
        // Some JVMs return null for File.list() when the
        // directory is empty.
        if (list != null) {
            for (int i = 0; i < list.length; i++) {
                File entry = new File(directory, list[i]);
                if (entry.isDirectory()) {
                    sizeInBytes = getSize(entry,sizeInBytes);
                } else {
                    sizeInBytes +=entry.length();
                }
            }
        }
    }
    return sizeInBytes;
}

MB = 1024 * 1024 ,即可获得 SizeInMB 除以 sizeInBytes 由MB的大小; sizeInMB = sizeInBytes / MB;

One MB=1024*1024,You can get SizeInMB by dividing sizeInBytes by size of MB; sizeInMB=sizeInBytes/MB;

这篇关于检索URL(Internet)的所有图像并保存在本地(SD卡) - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆