Search code examples
androidandroid-2.3-gingerbread

Download a file issue in android 2.3


I'm using this code to download from a URL ,it works great with android 4,but in the other hand it doesn't work with android 2.3. Can someone tell what have i done wrong ?

      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();

Solution

  • It works for me. Here is my method:

    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;
    }
    

    I hope I have helped you !