- Download Specific actions
- private void Download () {
- try {
- URL url = new URL (downloadurl);
- Open connection
- URLConnection conn = url.openconnection ();
- Open input Stream
- InputStream is = conn.getinputstream ();
- Get length
- int contentlength = conn.getcontentlength ();
- LOG.E (TAG, "contentlength =" + contentlength);
- Create a folder Mydownload, under the memory card
- String dirName = environment.getexternalstoragedirectory () + "/mydownload/";
- File file = new file (DirName);
- does not exist create
- if (!file.exists ()) {
- File.mkdir ();
- }
- The file name after download
- String fileName = dirName + "xiaomibianqian.apk";
- File file1 = new File (FileName);
- if (file1.exists ()) {
- File1.delete ();
- }
- Create a byte stream
- byte[] bs = new byte[1024];
- int Len;
- OutputStream os = new FileOutputStream (FileName);
- Write Data
- while (len = is.read (BS))! =-1) {
- Os.write (BS, 0, Len);
- }
- Close the stream when finished
- LOG.E (TAG, "download-finish");
- Os.close ();
- Is.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
- String dirName = environment.getexternalstoragedirectory () + "/mydownload/";
-
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android: Name= "Android.permission.READ_EXTERNAL_STORAGE"/>
Android downloads files to sd card based on URL