編輯:關於Android編程
String directoryName = Environment.getExternalStorageDirectory().toString() + "/filename";////文件保存路徑 ///傳入參數:Context對象,下載地址, 文件保存路徑; DownloadTask downloadTask = new DownloadTask (this, mDownloadUrl, directoryName); new Thread(downloadTask ).start();///////啟動線程進行下載
////下載類 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(); }; ////下載方法 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 = httpConnection.getContentLength(); Log.d(TAG, "totalSize, " + mTotalSize); if (mDownloadedSize == mTotalSize ) { ////////已下載到本地 return true; } 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); ////////下載百分百mDownloadPercent count = 0; } mDownloadedSize += offset; } if (mDownloadedSize == mTotalSize ) { /////////下載完成 } Log.d(TAG, "download finished."); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (inStream != null) { inStream.close(); } if (httpConnection != null) { httpConnection.disconnect(); } if (randomAccessFile != null) { randomAccessFile.close(); } } catch (Exception e) { } } } }
看了很多大神們的文章,感覺受益良多,也非常欣賞大家的分享態度,所以決定開始寫Blog,給大家分享自己的心得。先看看效果圖:本來准備在ListView的每個Item的布局上
之前的一遍學習筆記主要就Android滑動沖突中,在不同方向的滑動所造成沖突進行了了解,這種沖突很容易理解,當然也很容易解決。今天,就同方向的滑動所造成的沖突進行一下了解
1.效果這個效果在很多App裡都可以用到,基本上就已經泛濫了.這裡就記錄一下如何實現這一種效果.(截圖沒注意大小,丟幀也嚴重,所以看上去有點卡頓)2.實現步驟2.1布局文
Filter閱讀是我最近寫的一個Android的閱讀器,用於看英文的書(中文暫時不支持,會亂碼,下一版再改).在這裡開源給大家(Android studio的)源碼下載地