Android supports multiple file download classes and android file types.

Source: Internet
Author: User

Android supports multiple file download classes and android file types.

String directoryName = Environment. getExternalStorageDirectory (). toString () + "/filename"; // file storage path // input parameter: Context object, file storage path; DownloadTask downloadTask = new DownloadTask (this, mDownloadUrl, directoryName); new Thread (downloadTask ). start (); // start the thread to download

/// Download class public class DownloadTask implements Runnable {private long mDownloadedSize = 0; private long mTotalSize; private int mDownloadPercent; private String mLocalPath; private String mURL; private Context mContext; public DownloadTask (Context context, String url, String localPath) {this. mLocalPath = localPath; this. mURL = url; this. mContext = context ;}@ Override public void run () {download (); }; // Download method: protected boolean download () {File file = new File (mLocalPath); if (file. exists () {mDownloadedSize = file. length ();} else {mDownloadedSize = 0;} Log. d (TAG, "mURL," + mURL + "downloadedSize," + mDownloadedSize); HttpURLConnection httpConnection = null; URL url = null; try {url = new URL (mUpgradeURL ); httpConnection = (HttpURLConnection) url. openConnection (); mTotalSize = httpC Onnection. getContentLength (); Log. d (TAG, "totalSize," + mTotalSize); if (mDownloadedSize = mTotalSize) {/////// return true if it has been downloaded to the local device ;} else if (mDownloadedSize> mTotalSize) {if (! File. delete () {return false ;}} httpConnection. disconnect ();} catch (Exception e) {e. printStackTrace (); return false;} finally {try {if (httpConnection! = Null) {httpConnection. disconnect () ;}} catch (Exception e) {}} InputStream inStream = null; RandomAccessFile randomAccessFile = null; try {httpConnection = (HttpURLConnection) url. openConnection (); httpConnection. setRequestProperty ("Accept", "image/gif," + "image/jpeg," + "image/pjpeg," + "image/pjpeg, "+" application/x-shockwave-flash, "+" application/xaml + xml, "+" application/vnd. ms- Xpsdocument, "+" application/x-ms-xbap, "+" application/x-ms-application, "+" application/vnd. ms-excel, "+" application/vnd. ms-powerpoint, "+" application/msword, "+" */* "); httpConnection. setRequestProperty ("Accept-Language", "zh-CN"); httpConnection. setRequestProperty ("Referer", mUpgradeURL); httpConnection. setRequestProperty ("Charset", "UTF-8"); httpConnection. setRequestProperty ("Range ", "Bytes =" + mDownloadedSize + "-"); httpConnection. setRequestProperty ("Connection", "Keep-Alive"); inStream = httpConnection. getInputStream (); File saveFile = new File (mLocalPath); randomAccessFile = new RandomAccessFile (saveFile, "rwd"); randomAccessFile. seek (mDownloadedSize); int offset = 0; int count = 0; int perUnit = (int) mTotalSize/1024/100; byte [] buffer = new byte [1024]; while (offset = InStream. read (buffer, 0, 1024 ))! =-1) {randomAccessFile. write (buffer, 0, offset); count ++; if (count = perUnit & mDownloadedSize <mTotalSize) {mDownloadPercent = (int) (mDownloadedSize * 100/mTotalSize ); //// // download mDownloadPercent count = 0;} mDownloadedSize + = offset;} if (mDownloadedSize = mTotalSize) {// download completed} Log. d (TAG, "download finished. "); return true;} catch (Exception e) {e. printStackTrace (); retu Rn false;} finally {try {if (inStream! = Null) {inStream. close ();} if (httpConnection! = Null) {httpConnection. disconnect ();} if (randomAccessFile! = Null) {randomAccessFile. close () ;}} catch (Exception e ){}}}}



Android file type

APK
Put ("apk", "application/vnd. android. package-archive ");

Put ("atom", "application/atom + xml ");
Put ("xml", "application/atom + xml ");
Put ("json", "application/json ");
Put ("js", "application/javascript ");
Put ("ogg", "application/ogg ");

What file formats does Android support?

Download the simulator .. JAR. Many supported by NEC ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.