在我的Android应用程序中,我正在成功上传视频文件,但如果文件大于10mB,应用程序崩溃并抛出以下异常:
03-18 01:18:20.373: E/AndroidRuntime(5131): FATAL EXCEPTION: AsyncTask #4
03-18 01:18:20.373: E/AndroidRuntime(5131): java.lang.RuntimeException: An error occured while executing doInBackground()
03-18 01:18:20.373: E/AndroidRuntime(5131): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-18 01:18:20.373: E/AndroidRuntime(5131): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.lang.Thread.run(Thread.java:841)
03-18 01:18:20.373: E/AndroidRuntime(5131): Caused by: java.lang.OutOfMemoryError
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
03-18 01:18:20.373: E/AndroidRuntime(5131): at libcore.net.http.RetryableOutputStream.write(RetryableOutputStream.java:61)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.io.DataOutputStream.write(DataOutputStream.java:98)
03-18 01:18:20.373: E/AndroidRuntime(5131): at com.example.kroosky.HttpFileUpload.Sending(HttpFileUpload.java:116)
03-18 01:18:20.373: E/AndroidRuntime(5131): at com.example.kroosky.HttpFileUpload.Send_Now(HttpFileUpload.java:36)
03-18 01:18:20.373: E/AndroidRuntime(5131): at com.example.kroosky.VideoUpload.uploadFile(VideoUpload.java:599)
03-18 01:18:20.373: E/AndroidRuntime(5131): at com.example.kroosky.VideoUpload$getHashTagID.doInBackground(VideoUpload.java:666)
03-18 01:18:20.373: E/AndroidRuntime(5131): at com.example.kroosky.VideoUpload$getHashTagID.doInBackground(VideoUpload.java:1)
03-18 01:18:20.373: E/AndroidRuntime(5131): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-18 01:18:20.373: E/AndroidRuntime(5131): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-18 01:18:20.373: E/AndroidRuntime(5131): ... 4 more
这是我的HttpFileUpload类:
public class HttpFileUpload implements Runnable{
URL connectURL;
String responseString, token, Title, HashTag;
int Categorie;
byte[ ] dataToServer;
FileInputStream fileInputStream = null;
HttpFileUpload(String urlString, String vTitle, int vCategorie, String vHash, String token){
try{
connectURL = new URL(urlString);
Title= vTitle;
Categorie = vCategorie;
HashTag = vHash;
this.token = token;
}catch(Exception ex){
Log.i("HttpFileUpload","URL Malformatted");
}
}
boolean Send_Now(FileInputStream fStream, String fileName){
fileInputStream = fStream;
boolean uploadResult = Sending(fileName);
return uploadResult;
}
boolean Sending(String fileName){
String iFileName = fileName;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="fSnd";
try
{
Log.e(Tag,"Starting Http File Sending to URL");
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"videoTitle\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Title);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"videoCategorie\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(String.valueOf(Categorie));
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"videoHashTag\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(HashTag);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"token\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(this.token);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + iFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
//int maxBufferSize = 1 * 1024 * 1024;
int maxBufferSize = 1024;
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.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while( (ch = is.read()) != -1 ){
b.append( (char)ch );
}
String s = b.toString();
Log.i("Response",s);
dos.close();
return true; //Upload result
} catch (MalformedURLException ex){
Log.e(Tag, "URL error: " + ex.getMessage(), ex);
return false; //Upload result
} catch (IOException ioe){
Log.e(Tag, "IO error: " + ioe.getMessage(), ioe);
return false; //Upload result
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
我的videoUpload uploadFile函数:
public void uploadFile(String sourceFileUri) {
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog = ProgressDialog.show(VideoUpload.this, "", getString(R.string.uploadingVideo), true);
}
});
try {
videoTitle = (EditText) findViewById(R.id.videoTitleUpload);
String videoTitleTxt = videoTitle.getText().toString();
String videoCategorieTxt = categoriesSpinner.getSelectedItem()
.toString();
if (videoCategorieTxt.equalsIgnoreCase("funny")) {
videoCategorieCode = 0;
} else if (videoCategorieTxt.equalsIgnoreCase("shock")) {
videoCategorieCode = 1;
} else if (videoCategorieTxt.equalsIgnoreCase("emotion")) {
videoCategorieCode = 2;
} else if (videoCategorieTxt.equalsIgnoreCase("fail")) {
videoCategorieCode = 3;
} else if (videoCategorieTxt.equalsIgnoreCase("comedy")) {
videoCategorieCode = 4;
} else if (videoCategorieTxt.equalsIgnoreCase("sport")) {
videoCategorieCode = 5;
} else if (videoCategorieTxt.equalsIgnoreCase("fashion")) {
videoCategorieCode = 6;
} else if (videoCategorieTxt.equalsIgnoreCase("illusion")) {
videoCategorieCode = 7;
} else if (videoCategorieTxt.equalsIgnoreCase("cars")) {
videoCategorieCode = 8;
} else if (videoCategorieTxt.equalsIgnoreCase("bikes")) {
videoCategorieCode = 9;
} else if (videoCategorieTxt.equalsIgnoreCase("music")) {
videoCategorieCode = 10;
} else if (videoCategorieTxt.equalsIgnoreCase("stars video")) {
videoCategorieCode = 11;
} else if (videoCategorieTxt.equalsIgnoreCase("student")) {
videoCategorieCode = 12;
} else if (videoCategorieTxt.equalsIgnoreCase("holidays")) {
videoCategorieCode = 13;
}
final String fileName = sourceFileUri; // File path
// Set your file path here
FileInputStream fstrm = new FileInputStream(fileName);
// Set your server page url (and the file title/description)
HttpFileUpload hfu = new HttpFileUpload(upLoadServerUri,
videoTitleTxt, videoCategorieCode, videoHashTagID, token);
if (hfu.Send_Now(fstrm, fileName)) { // Video successfully uploaded
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.dismiss();
Toast.makeText(getApplicationContext(),
getString(R.string.videoUploadOk),
Toast.LENGTH_LONG).show();
}
});
Intent reloadVideos = new Intent(getApplicationContext(),
OnlineVideos.class);
reloadVideos.putExtra("token", token);
startActivity(reloadVideos);
finish();
} else { // Error uploading video
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.dismiss();
Toast.makeText(getApplicationContext(),
getString(R.string.videoUploadError),
Toast.LENGTH_LONG).show();
}
});
}
} catch (FileNotFoundException e) {
// Error: File not found
}
} // Close uploadVideo
有什么问题?我一直在考虑这个,但我无法理解。 请帮忙!