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://----------"); 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---