1

我正在使用此代码从 URL 下载,它适用于 android 4,但另一方面它不适用于 android 2.3。有人能告诉我我做错了什么吗?

      URL url = new URL(sUrl);

      URLConnection connection = url.openConnection();         

      connection.connect();                    
      InputStream input = new BufferedInputStream(url.openStream());

      OutputStream output = new FileOutputStream(pathFolder+"/"+fileName);

                 byte data[] = new byte[1024];
                 long total = 0;
                 int count;
                 while ((count = input.read(data)) != -1) {
                     total += count;
                     // publishing the progress....
                     publishProgress((int) (total * 100 / fileLength));
                     output.write(data, 0, count);
                 }

                 output.flush();
                 output.close();
                 input.close();
4

1 回答 1

0

这个对我有用。这是我的方法:

private boolean dowloadFile(String url, File saveFile) {
    int BUFF_SIZE = 1024 * 1024; //1Mo
    long length = 0;
    long current = 0;
    if(saveFile.exists())
        current = saveFile.length();
    try {
        DefaultHttpClient http = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        if(current>0)
            request.addHeader("Range", "bytes=" + current + "-");
        HttpResponse response = http.execute(request);
        if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 206) {
            return false;
        }

        Header[] headers = response.getHeaders("Content-Range");
        if(headers.length>0) {
            String s = headers[0].getValue();
            length = Integer.valueOf(s.subSequence(s.indexOf("/")+1, s.length()).toString());
        } else {
            Header[] headers2 = response.getHeaders("Content-Length");
            if(headers2.length>0)
                length = Integer.valueOf(headers2[0].getValue());

            if(current>0) {
                saveFile.delete();
                current = 0;
            }
        }

        BufferedInputStream ls = new BufferedInputStream(response.getEntity().getContent());

        long nexttime = 0;

        RandomAccessFile fos = new RandomAccessFile(saveFile, "rw");
        fos.seek(current);

        byte[] buffer = new byte[BUFF_SIZE];
        while (current < length) {
            boolean buffFull = false;
            int currentBuff = 0;
            int readSize = 0;
            while (buffFull == false) {
                readSize = ls.read(buffer, currentBuff, BUFF_SIZE - currentBuff);
                if (readSize == -1)
                    buffFull = true;
                else {
                    currentBuff += readSize;
                    if (currentBuff == BUFF_SIZE)
                        buffFull = true;
                }
            }
            fos.write(buffer, 0, currentBuff);
            current += currentBuff;
            long time = SystemClock.elapsedRealtime();
            if (nexttime < time) {
                // Progress
                nexttime = time + 1000;
            }
        }
        fos.close();
        // Progress Finish
    } catch(Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

我希望我对你有所帮助!

于 2013-05-23T18:57:21.270 回答