I try to download video from server which is big video around 250mb to 300mb in my android application. my code work fine for small video around 30mb to 40mb but I got exception when I download big video. The exception is given bellow:
10-28 10:11:57.222: D/Error....(4617): java.io.IOException: unexpected end of stream 10-28 10:11:57.227: W/System.err(4617): java.io.IOException: unexpected end of stream 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): at 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): at 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)
I try to find solution on stack over flow using this link link but I still get error above on large size video.
my code is given bellow
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;
}
}
If any suggestion then most well come. Thank you