Hi Friends,

I want to play an mp3 file as soon as my application starts. The mp3 file is
really large in size(approx 14 mb).

So I am trying to download it from a webserver and than I want to store it
on sdcard.

While doing so I am getting OutOfMemoryException. Can anybody please help
me?

My code is as follows:

>>>>>>>>>>>>>>>

public MainClass() throws Exception
{

URL u = new URL("http://----------";);
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);


*// Getting OutOfMemoryException here

**byte [] data=new byte[(int)contentLength];*


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();

}


Mahesh

--~--~---------~--~----~------------~-------~--~----~
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