Android Forums

Go Back   These FORUMS ARE DISABLED!! OUR NEW FORUMS ARE AT http://androidforum.com/ > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 06-29-2009, 09:55 PM   #1
Junior Member
 
Join Date: Jun 2009
Posts: 1
Friends: 0
View vikram.patil's Profile   View vikram.patil's Photo Album   View vikram.patil's Blog   View Social Groups
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 :

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 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing files mr vip Android Chat 2 03-11-2009 03:36 AM
3.5mm stereo audio connector gaborg Android Hardware 7 02-09-2009 02:13 AM
Bluetooth audio pft573 Android Hardware 1 12-24-2008 11:13 PM
Document files cz7asm Android Chat 2 11-25-2008 04:48 PM
Streaming Audio/a challenge wbexpress Android Development, Answers, Tutorials, and Code Snippets 2 11-20-2008 04:16 PM


Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 04:22 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.