我尝试在我的android应用程序中从服务器下载大约250mb到300mb的视频。我的代码适用于30mb到40mb的小视频,但我在下载大视频时遇到了异常情况。该异常是波纹管式给出:在Android下载视频时得到意外的“流的意外结束”
10-28 10:11:57.222:d /错误....(4617):产生java.io.IOException:流 意外结束10-28 10:11: 57.227:W/System.err(4617):java.io.IOException:流的意外结束 10-28 10:11:57.232:W/System.err(4617):在libcore.net.http.FixedLengthInputStream.read (FixedLengthInputStream.java:48) 10月10日至28日:11:57.232:W/System.err的(4617):在java.io.BufferedInputStream.read(BufferedInputStream.java:304) 10月10日至28日:11:57.237 :W/System.err(4617):在java.io.InputStream.read(InputStream.java:163) 10-28 10:11:57.237:W/System.err(4617):at com.photography.khalid .PreviewVideo.DownloadFile(PreviewVideo.java:1079) 10月10日至28日:11:57.237:W/System.err的(4617):在com.photography.khalid.PreviewVideo $ DownLoadVideoFile.doInBackground(PreviewVideo.java:1121) 10月10日至28日:11:57.242:W/System.err的(4617):在com.photography.khalid.PreviewVideo $ DownLoadVideoFile.doInBackground(PreviewVideo.java:1) 10月10日至28日:11:57.242:W/System.err的(4617):在android.os。的AsyncTask $ 2.call(AsyncTask.java:264) 10月10日至28日:11:57.242:W/System.err的(4617):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305) 10-28 10:11:57.242:W/System.err(4617):在java.util.concurrent.FutureTask.run(FutureTask.java:137) 10-28 10:11:57.247:W/System.err (4617):在android.os.AsyncTask $ $ SerialExecutor 1.run(AsyncTask.java:208) 10月10日至28日:11:57.247:W/System.err的(4617):在java.util.concurrent.ThreadPoolExecutor中。 runWorker(ThreadPoolExecutor.java:1 076) 10月10日至28日:11:57.252:W/System.err的(4617):在java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:569) 10月10日至28日:11:57.252: W/System.err的(4617):在java.lang.Thread.run(Thread.java:856)
我尝试使用这个链接 link找过流堆栈解决方案,但我仍然得到上述错误在大尺寸的视频。
我的代码波纹管
public void DownloadFile(String fileURL, String fileName) {
try {
String RootDir = Environment.getExternalStorageDirectory()
+ File.separator + "Photography";
/*String RootDir = context.getCacheDir()
+ File.separator + "Photography";*/
File RootFile = new File(RootDir);
RootFile.mkdir();
// File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
System.setProperty("http.keepAlive", "false");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
//Open a connection to that URL.
//URLConnection c = u.openConnection();
c.setUseCaches(false);
c.setDoInput(true);
c.setRequestMethod("POST");
c.setDoOutput(true);
c.connect();
int contentByte=c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(RootFile,
"temp"+fileName));
//f=openFileOutput("temp"+fileName, context.MODE_PRIVATE);
InputStream in = c.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(in, 1024 * 10);
byte[] buffer = new byte[10 * 1024];
int len1 = 0;
while ((len1 = inStream.read(buffer)) != -1) {
f.write(buffer, 0, len1);
Log.d("file data", buffer.toString()+",value of progressBarStatus"+progressBarStatus);
progressBarStatus+=(len1*100.0)/(contentByte)+0.0;
progressBarHorizontal.setProgress((int)progressBarStatus);
progressBar2.setProgress((int)progressBarStatus);
if(isTaskCancelled)
{
failToDownload=1;
break;
}
}
f.flush();
f.close();
inStream.close();
} catch (Exception e) {
Log.d("Error....", e.toString());
e.printStackTrace();
failToDownload=1;
}
}
如果给出任何建议,那么最幸福来了。谢谢
施与。与你的阙相关。 ! – 2015-04-21 11:34:08
由于视频大小有些时间服务器连接,我们因为像互联网低而且我们无法下载它的任何原因而丢失,所以我们使用库来下载他们管理所有我们需要的视频,并使用我共享的链接解决了我的问题。 – bhavikkumar 2015-10-13 10:13:10