简体   繁体   English

加快图像下载时间

[英]Speed up image download time

in my app I'm downloading images I get from an URL. 在我的应用程序中,我正在下载从URL获取的图像。 The problem here is that it takes wayy too long. 这里的问题是它需要太长时间。 The images are compressed and only 9km big. 图像被压缩,只有9公里大。 I'm downloading 50 of them, so let's say 500kByte of images are being downloaded by the app. 我正在下载50个,所以让我们说应用程序正在下载500kByte的图像。 This should be pretty fast, right? 这应该很快,对吗? Well, it isn't. 嗯,事实并非如此。 I'm sometimes wating for 20 seconds until it's loaded. 我有时会在加载之前等待20秒。 What is taking so long there? 什么花了那么久? I have a pretty easy way to download the images. 我有一个非常简单的方法来下载图像。 Here it is 这里是

int downloadingImages = getDownloadImageCount();
        System.out.println(downloadingImages);
        if(downloadingImages == 0){
            for(int c = downloadingImages; c < 50; c++){
                try{                    

                        bitmapArr[c]        = getBitmapFromURL(imageURLArr[c]);
                        setDownloadImageCount(50);
                        publishProgress(c);
                    } catch (FileNotFoundException f1){
                        Log.v("FileNotFoundException", "Bitmap konnte nicht geladen werden");
                } catch(NullPointerException e){

                } catch (IllegalStateException ie){

                } 
                setDownloadImageCount(50);
            }
        }

This is the getBitmapFromUrl function 这是getBitmapFromUrl函数

public static Bitmap getBitmapFromURL(String src) throws FileNotFoundException {
    try {
        //Downloading the image
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        try{
            //Saving the image to a bitmap and return it
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
            //Catch Exception if file not found
        } catch(FileNotFoundException ffe){
            Log.v("FileNotFoundException", "getBitmapFromURLMethod");
            return null;
        }        
        //Catch Exception if error at input and output
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } 
}

I mean, what is taking so long there? 我的意思是,那里花了那么久? How can I increase the speed. 我怎样才能提高速度。 I will use cahing for images later, but still... I mean it's 500kbs and takes so long. 我稍后会使用cahing图像,但仍然...我的意思是它是500kbs并且需要很长时间。 Why is that? 这是为什么?

Its completely natural that it takes a while. 它完全自然需要一段时间。 For each image you load from a URL a new HTTP-request is made (that is a request is sent to the server and it responds). 对于从URL加载的每个图像,都会生成一个新的HTTP请求(即请求被发送到服务器并响应)。

Thats 50 times the round-trip time over the network (not even including the time it may take the server to respond). 这是网络往返时间的50倍(甚至不包括服务器响应的时间)。 You should cache the images locally once you have them retrieved. 检索后,您应该在本地缓存图像。

The longest step in the process is probably sending a request to the server and waiting for it to respond, and you're repeating this step 50 times. 该过程中最长的步骤可能是向服务器发送请求并等待它响应,并且您将重复此步骤50次。 Do you have the ability to modify the server? 你有能力修改服务器吗? If so, consider adding an action that lets you download all 50 images at once (perhaps by putting them into a zip file, then unpacking them client-side). 如果是这样,请考虑添加一个动作,让您一次下载所有50个图像(可能将它们放入zip文件,然后在客户端解压缩)。

Try to wrap your InputStream to BufferedInputStream 尝试将InputStream包装到BufferedInputStream

BufferedInputStream bs = new BufferedInputStream(input);
Bitmap myBitmap = BitmapFactory.decodeStream(input);

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

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