Hi,

The file you are downloading is simply to large to fit in the
available RAM. Instead of downloading the entire file and then write
it to disk, download the file in small batches (of 16 kB for instance)
and write each batch to the SD card right away.



On Mon, Jan 26, 2009 at 4:25 AM, parth <parth.india...@gmail.com> wrote:
>
> I am downloading a audio file from server on sd card. It is showing me
> OutOfMemoryException.
>
> This is my code..
> ---------------------------
> package com.android;
>
> import java.io.BufferedInputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.net.URL;
> import java.net.URLConnection;
>
> public class MainClass {
>
>  public MainClass() throws Exception
>  {
>
>    URL u = new URL("http://test.cfmdeveloper.com/android/music.mp3";);
>    URLConnection uc = u.openConnection();
>    String contentType = uc.getContentType();
>    int contentLength = uc.getContentLength();
>    if(contentLength < Integer.MAX_VALUE)
>    {
>
>    }
>    if (contentType.startsWith("text/") || contentLength == -1) {
>      throw new IOException("This is not a binary file.");
>    }
>    InputStream raw = uc.getInputStream();
>    InputStream in = new BufferedInputStream(raw);
>    byte [] data=new byte[(int)contentLength];
>    //byte[] data1 = new byte[contentLength/2];
>    int bytesRead = 0;
>    int offset = 0;
>    while (offset < contentLength)
>    {
>
>      bytesRead = in.read(data, offset, data.length - offset);
>      if (bytesRead == -1)
>        break;
>      offset += bytesRead;
>    }
>    in.close();
>
>    /*if (offset != contentLength) {
>      throw new IOException("Only read " + offset + " bytes; Expected
> " + contentLength + " bytes");
>    }*/
>
>
>   // String filename = u.getFile().substring(filename.lastIndexOf
> ('/') + 1);
>    FileOutputStream out = new FileOutputStream("/sdcard/
> music.mp3");
>    out.write(data);
>    out.flush();
>    out.close();
>  }
> }
>
>
>
> The error is at this line:
> byte [] data=new byte[(int)contentLength];
> This is because the size of the contentLength 16987867
> ------------
>
>
> I am trying since long and stuck up here..
> How to solve this exception? Pls help
>
>
>
>
> >
>



-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to