我在IntentService中下載文件時遇到致命異常。小文件下載正常。但9MB + - 文件是問題,它看起來像它永無止境的下載,即使在高速WIFI連接。幾分鐘後有一個致命的例外。問題部分是週期(條件線)。IntentService中的下載文件引發致命異常
請提醒一下?我失去了一些東西..
例外:
FATAL EXCEPTION: IntentService[DownloadService]
java.lang.OutOfMemoryError
at java.lang.String.<init>(String.java:432)
at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
at java.lang.StringBuffer.toString(StringBuffer.java:723)
at com.splunk.mint.network.io.InputStreamMonitor.updateBody(InputStreamMonitor.java:123)
at com.splunk.mint.network.io.InputStreamMonitor.read(InputStreamMonitor.java:77)
at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
at java.io.InputStream.read(InputStream.java:163)
at cz.ppmfactum.android_smartaudit.application.application.connection.DownloadService.download(DownloadService.java:103)
at cz.ppmfactum.android_smartaudit.application.application.connection.DownloadService.onHandleIntent(DownloadService.java:46)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
代碼:
private synchronized void download(Intent intent) throws Exception {
String urlString = intent.getStringExtra("url");
Log.e("URL DOWNLOAD", urlString);
String path = intent.getStringExtra("path");
String name = intent.getStringExtra("name") + ".temp";
FileOutputStream output;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
File rootFile = new File(path);
if (!rootFile.exists())
rootFile.mkdirs();
File outputFile = new File(rootFile, name);
if (outputFile.exists()) outputFile.delete();
output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
FileControler.renameFile(outputFile, intent.getStringExtra("name"));
downloadedFile = new File(outputFile.getParent(), intent.getStringExtra("name"));
output.flush();
output.close();
input.close();
}
詢問'com.splunk.mint.network.io.InputStreamMonitor'的作者,因爲它是他們的代碼崩潰。 – CommonsWare 2014-09-22 14:48:14
Ou ..謝謝:) – 2014-09-22 14:58:15