1

working on my first real android app and one part of it is a download manager.

it must download video files that the app uses to teach people Gaelic.

it works fine on all but 3 files, it downloads bigger files and smaller files. but will not download colours.mp4 1.86MB, weekdays 1.53 MB or numbers 1.99 MB all the files between 1MB and 2MB.

it will just stop at the end of the download loop no errors for a few minutes just looking like its waiting to continue downloading then it will give an error "unexpected end of stream" can anyone suggest what the problem could be please?

I have crated the same app for the iPhone and that app has not got the same problem with these files.

this is the download loop called from inside an asynctask.

protected void download(String where, String file){

        try {
        //makes output file 
        OutputStream output = new FileOutputStream(getFilesDir()
                .getAbsolutePath() + "/vidos/" +file);


        int count=0;

        //gets url to download from
        URL url = new URL(where);

        URLConnection conection = url.openConnection();
        conection.connect();

        //gets the length of the file to work out percent downloaded 
        int lengthOfFile = conection.getContentLength();

        InputStream input = null;
        Log.v("downloading", String.valueOf(showprg));
        input = new BufferedInputStream(url.openStream());
        byte data[] = new byte[lengthOfFile];
        long total = 0;
        Log.v("downloading", "size: " + String.valueOf(downloading));
        while ((count = input.read(data)) > 0 && downloading) {
            total += count;
            publishProgress(String
                    .valueOf((int) ((total * 100) / lengthOfFile)));
            output.write(data, 0, count);

            //this is where it brakes
        };
        Log.v("publishProgress", "done");
        output.flush();
        output.close();
        input.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
    }
alastair
  • 46
  • 9
  • check that your net connectivity or file is not corrupted. – Harshit Rathi Apr 08 '14 at 10:52
  • Thanks for your input. I have tried recreating the file and it has not helped. I'm not sure how it could be the connectivity as it downloaded the other videos that are saved in the same folder on the same server with no problems. – alastair Apr 08 '14 at 12:48
  • 1
    try this http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog. it will make help you. – Harshit Rathi Apr 08 '14 at 12:50
  • thanks Harshit Rathi. after looking at your link I changed my URLConnection to a HttpURLConnection and removed the BufferedInputStream and now its downloads without problem. – alastair Apr 08 '14 at 13:55

1 Answers1

0

I changed my URLConnection to a HttpURLConnection and removed the BufferedInputStream and now it downloads without problem.

thanks to Harshit Rathi for his link to Cristian's post

Community
  • 1
  • 1
alastair
  • 46
  • 9