• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Error uploading audio Files

hello,

i m trying to upload more than one audio Files, having size more than 4 mb each.
but i m getting this exception

06-29 18:22:40.739: ERROR/AndroidRuntime(727): Uncaught handler: thread Thread-9 exiting due to uncaught exception
06-29 18:22:40.751: ERROR/AndroidRuntime(727): java.lang.OutOfMemoryError
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:103)
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:234)
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection$HttpOutputStream.write(HttpURLConnection.java:652)
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at java.io.DataOutputStream.write(DataOutputStream.java:107)


The code i use for uploading files is :

[FONT=&quot]private[/FONT][FONT=&quot] [/FONT][FONT=&quot]boolean[/FONT][FONT=&quot] uploadFile(File file, String urlString) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]boolean[/FONT][FONT=&quot] isUploadOk = [/FONT][FONT=&quot]false[/FONT][FONT=&quot];[/FONT][FONT=&quot][/FONT]
[FONT=&quot] HttpURLConnection conn = [/FONT][FONT=&quot]null[/FONT][FONT=&quot];[/FONT][FONT=&quot][/FONT]
[FONT=&quot] DataOutputStream dos = [/FONT][FONT=&quot]null[/FONT][FONT=&quot];[/FONT][FONT=&quot][/FONT]
[FONT=&quot] FileInputStream fileInputStream = [/FONT][FONT=&quot]null[/FONT][FONT=&quot];[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]int[/FONT][FONT=&quot] maxBufferSize = 4096;[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]try[/FONT][FONT=&quot] {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// Open a HTTP connection to the URL[/FONT][FONT=&quot][/FONT]
[FONT=&quot] URL url = [/FONT][FONT=&quot]new[/FONT][FONT=&quot] URL(urlString);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] conn = (HttpURLConnection) url.openConnection();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// Allow Inputs[/FONT][FONT=&quot][/FONT]
[FONT=&quot] conn.setDoInput([/FONT][FONT=&quot]true[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// Allow Outputs[/FONT][FONT=&quot][/FONT]
[FONT=&quot] conn.setDoOutput([/FONT][FONT=&quot]true[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// Don't use a cached copy.[/FONT][FONT=&quot][/FONT]
[FONT=&quot] conn.setUseCaches([/FONT][FONT=&quot]true[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// Use a post method.[/FONT][FONT=&quot][/FONT]
[FONT=&quot] conn.setRequestMethod([/FONT][FONT=&quot]"PUT"[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] conn.setRequestProperty([/FONT][FONT=&quot]"Connection"[/FONT][FONT=&quot], [/FONT][FONT=&quot]"Keep-Alive"[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] conn.setRequestProperty([/FONT][FONT=&quot]"Content-Type"[/FONT][FONT=&quot],[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]"multipart/form-data;boundary="[/FONT][FONT=&quot] + [/FONT][FONT=&quot]boundary[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] dos = [/FONT][FONT=&quot]new[/FONT][FONT=&quot] DataOutputStream(conn.getOutputStream());[/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.writeBytes([/FONT][FONT=&quot]twoHyphens[/FONT][FONT=&quot] + [/FONT][FONT=&quot]boundary[/FONT][FONT=&quot] + [/FONT][FONT=&quot]lineEnd[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.writeBytes([/FONT][FONT=&quot]"Content-Disposition: form-data; name=\""[/FONT][FONT=&quot][/FONT]
[FONT=&quot] + file.getAbsolutePath() + [/FONT][FONT=&quot]"\";filename=\""[/FONT][FONT=&quot][/FONT]
[FONT=&quot] + file.getName() + [/FONT][FONT=&quot]"\""[/FONT][FONT=&quot] + [/FONT][FONT=&quot]lineEnd[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.writeBytes([/FONT][FONT=&quot]lineEnd[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] Log.e([/FONT][FONT=&quot]""[/FONT][FONT=&quot], [/FONT][FONT=&quot]"Headers are written"[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// create a buffer of maximum size[/FONT][FONT=&quot][/FONT]
[FONT=&quot] fileInputStream = [/FONT][FONT=&quot]new[/FONT][FONT=&quot] FileInputStream(file);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]int[/FONT][FONT=&quot] bytesAvailable = fileInputStream.available();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]int[/FONT][FONT=&quot] bufferSize = Math.min(bytesAvailable, maxBufferSize);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]byte[/FONT][FONT=&quot][] buffer = [/FONT][FONT=&quot]new[/FONT][FONT=&quot] [/FONT][FONT=&quot]byte[/FONT][FONT=&quot][bufferSize];[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// read file and write it into form...[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]int[/FONT][FONT=&quot] bytesRead = fileInputStream.read(buffer, 0, bufferSize);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]while[/FONT][FONT=&quot] (bytesRead > 0) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.write(buffer, 0, bufferSize);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] bytesAvailable = fileInputStream.available();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] bufferSize = Math.min(bytesAvailable, maxBufferSize);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] bytesRead = fileInputStream.read(buffer, 0, bufferSize);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.flush();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] buffer = [/FONT][FONT=&quot]null[/FONT][FONT=&quot];[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// send MULTIPART/FORM data necessary after file data.![/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.writeBytes([/FONT][FONT=&quot]lineEnd[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.writeBytes([/FONT][FONT=&quot]twoHyphens[/FONT][FONT=&quot] + [/FONT][FONT=&quot]boundary[/FONT][FONT=&quot] + [/FONT][FONT=&quot]twoHyphens[/FONT][FONT=&quot] + [/FONT][FONT=&quot]lineEnd[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]// close streams[/FONT][FONT=&quot][/FONT]
[FONT=&quot] Log.e([/FONT][FONT=&quot]"fileupload"[/FONT][FONT=&quot], [/FONT][FONT=&quot]"File is written"[/FONT][FONT=&quot]);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] } [/FONT][FONT=&quot]catch[/FONT][FONT=&quot] (MalformedURLException ex) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] Log.e([/FONT][FONT=&quot]"fileupload"[/FONT][FONT=&quot], [/FONT][FONT=&quot]"error: "[/FONT][FONT=&quot] + ex.getMessage(), ex);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]catch[/FONT][FONT=&quot] (IOException ioe) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] Log.e([/FONT][FONT=&quot]"Exception:: "[/FONT][FONT=&quot], [/FONT][FONT=&quot]"error: "[/FONT][FONT=&quot] + ioe.getMessage(), ioe);[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot]finally[/FONT][FONT=&quot][/FONT]
[FONT=&quot] {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]try[/FONT][FONT=&quot][/FONT]
[FONT=&quot] {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]if[/FONT][FONT=&quot](fileInputStream != [/FONT][FONT=&quot]null[/FONT][FONT=&quot])[/FONT][FONT=&quot][/FONT]
[FONT=&quot] fileInputStream.close();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot]catch[/FONT][FONT=&quot] (Exception e) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]try[/FONT][FONT=&quot][/FONT]
[FONT=&quot] {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]if[/FONT][FONT=&quot](dos != [/FONT][FONT=&quot]null[/FONT][FONT=&quot])[/FONT][FONT=&quot][/FONT]
[FONT=&quot] { [/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.flush();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]//dos.close();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot]catch[/FONT][FONT=&quot] (Exception e) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]try[/FONT][FONT=&quot][/FONT]
[FONT=&quot] {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot]if[/FONT][FONT=&quot](dos != [/FONT][FONT=&quot]null[/FONT][FONT=&quot])[/FONT][FONT=&quot][/FONT]
[FONT=&quot] {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] dos.close();[/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot]catch[/FONT][FONT=&quot] (Exception e) {[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT][FONT=&quot]return[/FONT][FONT=&quot] isUploadOk;[/FONT][FONT=&quot][/FONT]
[FONT=&quot] }[/FONT]




 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones