I'm trying to download files such as:

http://www.freesound.org/download/23246/23246_acclivity_GullsByTheSea.mp3

to the SDCARD of my t-mobile G1.

I can only download about 2^16 ~ (16384 bytes) before my input stream
read returns -1.

This wouldn't be a problem if the connection object could retrieve the
content's length, so that I would keep trying the read when the total
bytes have not yet been downloaded ... but the connection object
always returns -1 for the content length.

Any thoughts or tips...

My code is below:

private synchronized void download()
{
        HttpURLConnection conn = null;
        InputStream inStream = null;
        FileOutputStream outStream = null;

        try
        {
                //Where the file comes from
                URL sourceURL = new URL(m_url);

                conn = (HttpURLConnection) sourceURL.openConnection();
                conn.connect();

                inStream = conn.getInputStream();

                Log.d(TAG, "content length = " + conn.getContentLength()); // 
alwasy
returns -1

                // Where the file is going to go
                outStream = new FileOutputStream("/sdcard/Music/" + 
m_newFilename);

                //a read buffer
                byte[] bytes = new byte[1024];

                //read the first chunk
                int readBytes = 0;
                int total = 0;

                readBytes = inStream.read(bytes, 0, 1024);

                Log.d(TAG, "readBytes = " + readBytes);

                while (readBytes > 0)
                {
                        total += readBytes;

                        //Write the buffered chunk to the file
                        outStream.write(bytes, 0, readBytes);

                        Log.d(TAG, "total = " + total);

                        try
                        {
                                wait(100);
                        }
                        catch(InterruptedException ie){}

                        readBytes = inStream.read(bytes, 0, 1024);
                }
        }
        catch(Exception e)
        {
                Log.d(TAG, "Run exception: " + e.toString());
        }
        finally
        {
                Log.d(TAG, "Finally");
                try
                {
                        inStream.close();
                        outStream.close();
                }
                catch (IOException e){}
        }
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to