Go Back   Android Forums > Android Discussion > Android Developers

Android Developers Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old 06-30-2009, 12:02 AM   #1 (permalink)
Junior Member
 
Join Date: Jun 2009
Posts: 4
 
Device(s):
vikram.patil is on a distinguished road
Default 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(ByteArrayOutp utStream.java:103)
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at java.io.ByteArrayOutputStream.write(ByteArrayOutpu tStream.java:234)
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at org.apache.harmony.luni.internal.net.www.protocol. http.HttpURLConnection$HttpOutputStream.write(Http URLConnection.java:652)
06-29 18:22:40.751: ERROR/AndroidRuntime(727): at java.io.DataOutputStream.write(DataOutputStream.ja va:107)


The code i use for uploading files is :

privateboolean uploadFile(File file, String urlString) {

boolean isUploadOk = false;
HttpURLConnection conn = null;
DataOutputStream dos = null;
FileInputStream fileInputStream = null;

int maxBufferSize = 4096;

try {

// Open a HTTP connection to the URL
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();

// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(true);
// Use a post method.
conn.setRequestMethod("PUT");
conn.setRequestProperty("Connection", "Keep-Alive");

conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);

dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\""
+ file.getAbsolutePath() + "\";filename=\""
+ file.getName() + "\"" + lineEnd);
dos.writeBytes(lineEnd);

Log.e("", "Headers are written");

// create a buffer of maximum size
fileInputStream = new FileInputStream(file);
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);

byte[] buffer = newbyte[bufferSize];

// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
dos.flush();
}

buffer = null;

// send MULTIPART/FORM data necessary after file data.!
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// close streams
Log.e("fileupload", "File is written");


} catch (MalformedURLException ex) {
Log.e("fileupload", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe) {
Log.e("Exception:: ", "error: " + ioe.getMessage(), ioe);

}finally
{
try
{
if(fileInputStream != null)
fileInputStream.close();

}catch (Exception e) {

}

try
{
if(dos != null)
{
dos.flush();
//dos.close();
}

}catch (Exception e) {

}

try
{
if(dos != null)
{

dos.close();
}

}catch (Exception e) {

}

}



return isUploadOk;
}





vikram.patil is offline   Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Discussion > Android Developers
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 10:15 AM.
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0 RC2
vBulletin Skins by: Relivo Media