开发者

Android copying large inputstream to file very slow

I have an app that is downloading a zip file and then copying this file to a temporary file on the sd card on the phone, but it is being very very slow.

      InputStream in = new BufferedInputStream(url.openStream(), 1024);
        File tempFile = File.createTempFile("arc", ".zip", targetDir); //target dir is a file
        String tempFilePath = tempFile.getAbsolutePath();
        OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));

//copying file (in different void)    
        byte[] buffer = new byte[8192];
        int len;
        len = in.read(buffer);
enter code here

//it loops here f开发者_如何学Goor AGES
        while (len >= 0) {       
            out.write(buffer, 0, len);
            len = in.read(buffer);
                }
            in.close();
            out.close();

My file is about 20MB, initially I had the buffer size of 1024, and changed it to 8192 thinking it may speed it up but it seemed to make no difference? I always finishes, and I get no errors it just takes ages!

I have searched to try and find a solution but I'm not coming up with anything so I may be going about this totally the wrong way?

Can anyone see what I'm doing wrong?

Bex


Donot increase buffer size. That may cause your application MemoryOutOfBoundsException.

There are varous factors for which your download will be slow. Weak internet connection, Weak file transfer and receiveing mode is also responsible. It also depend on capacity of device. Check whether you are using following code to create inputstream

URL u = new URL("enter url url here");
                HttpURLConnection c = (HttpURLConnection) u.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
                InputStream in = c.getInputStream();

Thanks Deepak

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜