Error uploading audio Files

Put your problem here if it does not fit any of the other categories.

Error uploading audio Files

Postby vikram.patil » Tue Jun 30, 2009 4:41 am

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 :

private boolean 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 = new byte[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
Freshman
Freshman
 
Posts: 3
Joined: Tue Jun 30, 2009 4:30 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests