2

I have a problem i get -1 as length of the file. I tried a lot of time to fix it. I don't know why this happens. Also when i call again the asynctask , song1 stays the same(but the song1 value have not any problem, i tested with a toast and it works ). the 'song1' is a string and each time have different values. Thank you.

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);

            mProgressDialog.setMessage("Downloading "+song1+"..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
    }
}




protected String doInBackground(String... aurl) {
        int count;

        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            ((HttpURLConnection) conexion).setInstanceFollowRedirects(true);
            conexion.connect();


            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/download/"+song1);

            byte data[] = new byte[20480];

            long total = 0;


            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total/lenghtOfFile)*100));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {}
        return null;

    }

and here is my LogCat

03-08 20:25:17.714: D/ANDRO_ASYNC(14559): Lenght of file: -1
03-08 20:25:18.564: D/dalvikvm(14559): GC freed 18632 objects / 1179128 bytes in 112ms
03-08 20:25:18.864: I/global(14559): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
03-08 20:25:18.874: D/ANDRO_ASYNC(14559): -420000
03-08 20:25:18.954: D/ANDRO_ASYNC(14559): -700000
03-08 20:25:18.984: D/ANDRO_ASYNC(14559): -1820000
03-08 20:25:19.029: D/ANDRO_ASYNC(14559): -1960000
Jessy Jameson
  • 197
  • 2
  • 5
  • 15

2 Answers2

1
web.setDownloadListener { url, userAgent, contentDisposition, mimeType, contentLength ->
    val ul = url
    DownloadFile(this, url,contentLength).execute(ul)
}
Elletlar
  • 3,136
  • 7
  • 32
  • 38
Pro kashan
  • 21
  • 1
  • 2
  • 4
    Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – fcdt Nov 02 '20 at 16:56
0

Getting a length of -1 isn't uncommon. Usually that means that the server is sending it's buffer of data to you (likely because it is full), but it still does not know the entire size of the response. Or it's a lazy developer.

With a length of -1, you just need to keep loading information until you are at the end of the stream.

Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
  • i download normally with -1 length but the basic problem is that progress bar does not working. still stucked on 0% and after the download completed disappeared – Jessy Jameson Mar 08 '12 at 18:50
  • Well are you doing anything in your onProgressUpdate method with the progress bar? What's the value? I'm guessing it's a very large negative value, so staying at 0 makes sense to me. – Darren Kopp Mar 08 '12 at 19:15
  • take a look at this question on async task and progress: http://stackoverflow.com/questions/6450275/android-how-to-work-with-asynctasks-progressdialog – Darren Kopp Mar 08 '12 at 19:16
  • onProgressUpdate i have thisLog.d("ANDRO_ASYNC",progress[0]); mProgressDialog.setProgress(Integer.parseInt(progress[0])); – Jessy Jameson Mar 08 '12 at 19:19
  • oh yeah, i see those in your question now. yeah, that's why it's staying at 0. Those numbers are negative. When you get -1 as content length, you aren't going to be able to use progress bar. – Darren Kopp Mar 08 '12 at 19:20
  • i tried to download something else from my server but i get the same ...-1 again. i can't understand why – Jessy Jameson Mar 08 '12 at 19:29
  • 1
    Hey did you get solution for this.Please help, me also facing same problem – ShreeshaDas Aug 01 '13 at 12:24