java - Download mp4 from server and save it to sdcard -
i want download mp4 or .3gp files server android device. have tried multiple ways achieve in cases gives ioexception , time give protocolexception
first method download video using downloadvideotask.class
public class downloadvideotask extends asynctask<string, integer, boolean> { string nameofsong; context context; boolean flage = true; progressdialog progressdialog2; @suppresslint("inlinedapi") public downloadvideotask(context context,string tracktitle) { this.context = context; nameofsong = tracktitle; } @override protected void onpreexecute() { super.onpreexecute(); if(build.version.sdk_int >= build.version_codes.honeycomb) { progressdialog2 = new progressdialog(context, alertdialog.theme_holo_light); } else { progressdialog2 = new progressdialog(context); } progressdialog2.setindeterminate(false); progressdialog2.setmax(100); progressdialog2.settitle("please wait..."); try { progressdialog2.setmessage("downloding.. " + nameofsong.substring(0, 20)); } catch (exception e) { progressdialog2.setmessage("downloding song...." ); } progressdialog2.setprogressstyle(progressdialog.style_horizontal); progressdialog2.show(); } @override protected boolean doinbackground(string... params) { /* string tracktitle = params[0]; nameofsong = tracktitle; */ string trackurl = params[0]; try { file root;// = environment.getexternalstoragepublicdirectory(environment.directory_downloads); if(build.version.sdk_int < build.version_codes.honeycomb) { root = environment.getexternalstoragedirectory(); } else { root = environment.getexternalstoragepublicdirectory(environment.directory_downloads); } file dir = new file(root.getabsolutepath() + "/xardownloder"); if (dir.exists() == false) { dir.mkdirs(); } url url = new url(trackurl); file file = new file(dir, nameofsong); urlconnection urlconnection = url.openconnection(); int filelength = urlconnection.getcontentlength();//ye statement inputstream k bad likhi thi inputstream inputstream = urlconnection.getinputstream(); outputstream outputstream = new fileoutputstream( root.getabsolutepath() + "/xardownloder/" + nameofsong + ".mp4"); byte data[] = new byte[1024]; long total = 0; int count; while ((count = inputstream.read(data)) != -1) { total += count; // publishing progress.... publishprogress((int) (total * 100 / filelength)); outputstream.write(data, 0, count); } outputstream.flush(); outputstream.close(); inputstream.close(); try { if(build.version.sdk_int < build.version_codes.honeycomb) { context.sendbroadcast(new intent(intent.action_media_mounted, uri.parse("file://"+ environment.getexternalstoragedirectory()))); } else { context.sendbroadcast(new intent(intent.action_media_mounted,uri.parse("file://" + environment.getexternalstoragepublicdirectory(environment.directory_music)))); } context.sendbroadcast(new intent( intent.action_media_scanner_scan_file, uri .fromfile(file))); } catch (exception e) { } return true; } catch (ioexception e) { flage = false; e.printstacktrace(); } return false; } @override protected void onpostexecute(boolean result) { super.onpostexecute(result); if (result) { try { toast.maketext(context, nameofsong.substring(0, 30) + "downloaded...", toast.length_long).show(); } catch (exception e) { toast.maketext(context, "song downloaded...", toast.length_long).show(); } } else { toast.maketext(context, "sorry, song not downloadable", toast.length_long).show(); } progressdialog2.dismiss(); } @override protected void onprogressupdate(integer... values) { super.onprogressupdate(values); progressdialog2.setprogress(values[0]); } }
second method using downloadfile
public class downloadfile extends asynctask<string, integer, string> { string videotodownload = "http://r2---sn-u2oxu-f5f6.googlevideo.com/videoplayback?expire=1438261718&fexp=901816,9405637,9407538,9407942,9408513,9408710,9409172,9413020,9414764,9414856,9414935,9415365,9415485,9416126,9416355,9417009,9417719,9418201,9418204&id=d813f7f3bef428da&mn=sn-u2oxu-f5f6&mm=31&mime=video/mp4&upn=82uaibrk7em&itag=18&pl=24&dur=148.189&ip=167.114.5.145&key=yt5&ms=au&mt=1438239687&mv=u&source=youtube&ipbits=0&pcm2cms=yes&sparams=dur,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pcm2cms,pl,ratebypass,source,upn,expire&lmt=1428049239028653&signature=39087cbd9bdc9ebd612ca0e8e82ac692b427ffe3.18c23cd0aec8410cfbe4f35f532199dff21e7dfa&ratebypass=yes&sver=3&signature=&title=how+to+train+your+dragon+2+official+trailer+%231+%282014%29+-+animation+sequel+hd&filename=how_to_train_your_dragon_2_official_trailer_1_2014__animation_sequel_hd.mp4"; public downloadfile() { } @override protected string doinbackground(string... params) { int count; try { mp4load(videotodownload); } catch (exception e) { // todo: handle exception } /*try { url url = new url(videotodownload); urlconnection conexion = url.openconnection(); conexion.connect(); // useful can show tipical 0-100% // progress bar int lenghtoffile = conexion.getcontentlength(); // downlod file inputstream input = new bufferedinputstream(url.openstream()); outputstream output = new fileoutputstream("/sdcard/xarwere/firstdownload.mp4"); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing progress.... publishprogress((int) (total * 100 / lenghtoffile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (exception e) { e.printstacktrace(); }*/ return null; } public void mp4load(string urling) { try { url url = new url(urling); httpurlconnection con = (httpurlconnection) url.openconnection(); con.setrequestmethod("get"); //c.setdooutput(true); con.connect(); string path = environment.getexternalstoragedirectory() + "/download/"; //log.v(log_tag, "path: " + path); file file = new file(path); file.mkdirs(); string filename = "test.mp4"; file outputfile = new file(file, filename); if (!outputfile.exists()) { outputfile.createnewfile(); } fileoutputstream fos = new fileoutputstream(outputfile); int status = con.getresponsecode();//my doctory inputstream = con.getinputstream(); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fos.close(); is.close(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (protocolexception e) { e.printstacktrace(); } catch (ioexception e1) { e1.printstacktrace(); } } }
where videotodownload in downloadfile.class urli want download. @ inputstream gives exception.
and call these asynctask like
new downloadfile().execute(); new downloadvideotask(testingactivity.this, "nameofsong").execute("http://r2---sn-u2oxu-f5f6.googlevideo.com/videoplayback?expire=1438261718&fexp=901816,9405637,9407538,9407942,9408513,9408710,9409172,9413020,9414764,9414856,9414935,9415365,9415485,9416126,9416355,9417009,9417719,9418201,9418204&id=d813f7f3bef428da&mn=sn-u2oxu-f5f6&mm=31&mime=video/mp4&upn=82uaibrk7em&itag=18&pl=24&dur=148.189&ip=167.114.5.145&key=yt5&ms=au&mt=1438239687&mv=u&source=youtube&ipbits=0&pcm2cms=yes&sparams=dur,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pcm2cms,pl,ratebypass,source,upn,expire&lmt=1428049239028653&signature=39087cbd9bdc9ebd612ca0e8e82ac692b427ffe3.18c23cd0aec8410cfbe4f35f532199dff21e7dfa&ratebypass=yes&sver=3&signature=&title=how+to+train+your+dragon+2+official+trailer+%231+%282014%29+-+animation+sequel+hd&filename=how_to_train_your_dragon_2_official_trailer_1_2014__animation_sequel_hd.mp4");
i slighthly modified code, downloads file well. did add internet permission?
public class downloadfile extends asynctask<string, integer, string> { string videotodownload = "http://r2---sn-u2oxu-f5f6.googlevideo.com/videoplayback?expire=1438261718&fexp=901816,9405637,9407538,9407942,9408513,9408710,9409172,9413020,9414764,9414856,9414935,9415365,9415485,9416126,9416355,9417009,9417719,9418201,9418204&id=d813f7f3bef428da&mn=sn-u2oxu-f5f6&mm=31&mime=video/mp4&upn=82uaibrk7em&itag=18&pl=24&dur=148.189&ip=167.114.5.145&key=yt5&ms=au&mt=1438239687&mv=u&source=youtube&ipbits=0&pcm2cms=yes&sparams=dur,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pcm2cms,pl,ratebypass,source,upn,expire&lmt=1428049239028653&signature=39087cbd9bdc9ebd612ca0e8e82ac692b427ffe3.18c23cd0aec8410cfbe4f35f532199dff21e7dfa&ratebypass=yes&sver=3&signature=&title=how+to+train+your+dragon+2+official+trailer+%231+%282014%29+-+animation+sequel+hd&filename=how_to_train_your_dragon_2_official_trailer_1_2014__animation_sequel_hd.mp4"; @override protected string doinbackground(string... params) { int count; try { mp4load(videotodownload); } catch (exception e) { // todo: handle exception } return null; } public void mp4load(string urling) { try { url url = new url(urling); httpurlconnection con = (httpurlconnection) url.openconnection(); con.setrequestmethod("get"); //c.setdooutput(true); con.connect(); string downloadspath = environment.getexternalstoragepublicdirectory(environment.directory_downloads).getabsolutepath(); string filename = "test.mp4"; file outputfile = new file(downloadspath, filename); if (!outputfile.exists()) { outputfile.createnewfile(); } fileoutputstream fos = new fileoutputstream(outputfile); int status = con.getresponsecode(); inputstream = con.getinputstream(); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fos.close(); is.close(); } catch (ioexception e) { e.printstacktrace(); } } }
Comments
Post a Comment