0

Possible Duplicate:
Android download binary file problems

I'm unsuccessfully trying to download a file from the internet. When the service starts, the size of downloaded file freezes at 325 B. I have got the internet connection and the permission for accessing internet and writing on external storage.

public class DownloadService extends IntentService {

    public DownloadService() {
        super(null);
    }

        @Override
    protected void onHandleIntent(Intent arg0) {
        Log.d("service", "onHandleIntent()");
        try {
            URL fileUrl = new URL("http://goo.gl/Mfyya");

                HttpURLConnection urlConnection = (HttpURLConnection)fileUrl.openConnection();
                urlConnection.setRequestMethod("GET");
            //  urlConnection.setDoOutput(true);
                urlConnection.connect();

                File sdcard = new File (Environment.getExternalStorageDirectory()+"/my");
                File file = new File (sdcard, "filename.mp4");

                FileOutputStream fileoutput = new FileOutputStream (file);
                InputStream inputstream = urlConnection.getInputStream();

            //  int totalSize = urlConnection.getContentLength();
                int downloadedSize = 0;
                byte[] buffer = new byte[1024];
                int bufferlength =0;

                while ((bufferlength = inputstream.read(buffer)) >= 0)
                {   
                    fileoutput.write(buffer, 0, bufferlength);
                    downloadedSize += bufferlength;
                    //updateProgress(downloadedSize, totalSize);
                }
                fileoutput.close();
            } catch (IOException e) {
                //Log.d("service", "error");
                e.printStackTrace();
            }
    }
}

What is going wrong?

Community
  • 1
  • 1
Deve
  • 181
  • 4
  • 14

0 Answers0