我尝试在我的Android应用程序中从服务器下载大约250mb到300mb的视频。我的代码适用于大约30mb到40mb的小视频,但是当我下载大视频时我得到了例外。例外情况如下:
10-28 10:11:57.222:D / Error ....(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):at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:48) 10-28 10:11:57.232:W / System.err(4617):at java.io.BufferedInputStream.read(BufferedInputStream.java:304) 10-28 10:11:57.237:W / System.err(4617):at 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-28 10:11:57.237:W / System.err(4617):at com.photography.khalid.PreviewVideo $ DownLoadVideoFile.doInBackground(PreviewVideo.java:1121) 10-28 10:11:57.242:W / System.err(4617):at com.photography.khalid.PreviewVideo $ DownLoadVideoFile.doInBackground(PreviewVideo.java:1) 10-28 10:11:57.242:W / System.err(4617):在android.os.AsyncTask $ 2.call(AsyncTask.java:264) 10-28 10:11:57.242:W / System.err(4617):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 10-28 10:11:57.242:W / System.err(4617):at 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-28 10:11:57.247:W / System.err(4617):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 10-28 10:11:57.252:W / System.err(4617):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 10-28 10:11:57.252:W / System.err(4617):at 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;
}
}
如果有任何建议,那么最好的。谢谢
答案 0 :(得分:-1)
我发现很长的视频我们不能使用手动代码下载我们必须在这里使用下载管理器我共享链接,这有助于我完成任务