android多线程下载图片
生活随笔
收集整理的這篇文章主要介紹了
android多线程下载图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很多時候我們需要在Android設備上下載遠程服務器上的圖片進行顯示,今天Android123整理出兩種比較好的方法來實現遠程圖片的下載。?
? 方法一、直接通過Android提供的Http類訪問遠程服務器,這里AndroidHttpClient是SDK 2.2中新出的方法,API Level為8,大家需要注意下,靜態訪問可以直接調用,如果SDK版本較低可以考慮Apache的Http庫,當然HttpURLConnection 或URLConnection也可以。?
?? static Bitmap downloadBitmapByCwj(String url) {?
??? final AndroidHttpClient client = AndroidHttpClient.newInstance("Android123");?
??? final HttpGet getRequest = new HttpGet(url);?
??? try {?
??????? HttpResponse response = client.execute(getRequest);?
??????? final int statusCode = response.getStatusLine().getStatusCode();?
??????? if (statusCode != HttpStatus.SC_OK) {??
??????????? Log.e("cwjDebug", "Error " + statusCode + " while retrieving bitmap from " + url);??
??????????? return null;?
??????? }?
?????????
??????? final HttpEntity entity = response.getEntity();?
??????? if (entity != null) {?
??????????? InputStream inputStream = null;?
??????????? try {?
??????????????? inputStream = entity.getContent();??
??????????????? final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);?
??????????????? return bitmap;?
??????????? } finally {?
??????????????? if (inputStream != null) {?
??????????????????? inputStream.close();???
??????????????? }?
??????????????? entity.consumeContent();?
??????????? }?
??????? }?
??? } catch (Exception e) {?
????????? getRequest.abort();?
??????? Log.e("android123Debug", "Error while retrieving bitmap from " + url, e.toString());?
??? } finally {?
??????? if (client != null) {?
??????????? client.close();?
??????? }?
??? }?
??? return null;?
}?
? 這里Android開發網提醒大家,BitmapFactory類的decodeStream方法在網絡超時或較慢的時候無法獲取完整的數據,這里我們通過繼承FilterInputStream類的skip方法來強制實現flush流中的數據,主要原理就是檢查是否到文件末端,告訴http類是否繼續。?
static class FlushedInputStream extends FilterInputStream {?
??? public FlushedInputStream(InputStream inputStream) {?
??????? super(inputStream);?
??? }?
??? @Override?
??? public long skip(long n) throws IOException {?
??????? long totalBytesSkipped = 0L;?
??????? while (totalBytesSkipped < n) {?
??????????? long bytesSkipped = in.skip(n - totalBytesSkipped);?
??????????? if (bytesSkipped == 0L) {?
????????????????? int byte = read();?
????????????????? if (byte < 0) {?
????????????????????? break;? // we reached EOF?
????????????????? } else {?
????????????????????? bytesSkipped = 1; // we read one byte?
????????????????? }?
?????????? }?
??????????? totalBytesSkipped += bytesSkipped;?
??????? }?
??????? return totalBytesSkipped;?
??? }?
}?
? 方法二、AsyncTask異步任務?
? 從Android 1.5固件開始Google提供了一個AsyncTask類來幫助開發者處理異步下載的實現,相對于Thread而言他可以運行在UI線程中,其內部的實現是從Java 5開始的并發包concurrent中派生而來的,總體實現比較可靠就是資源占用略大了些。不過使用起來比簡單。這里下載圖片類 ImageDownloader類的download方法可以很好的處理實現UI顯示等操作,參數一url為遠程server上文件的url,第二個參數為imageview對象,可以直接讓imageview顯示出下載的遠程圖片。?
? public class ImageDownloader {?
??? public void download(String url, ImageView imageView) {?
??????????? BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);?
??????????? task.execute(url);?
??????? }?
??? }?
}?
有關具體的AsyncTask類實現,考慮到圖片可能較大,為了給JVM充分的空間存儲,這里Android123推薦大家使用弱引用來保存ImageView對象。
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {?
??? private String url;?
??? private final WeakReference<ImageView> imageViewReference;? //使用WeakReference解決內存問題?
??? public BitmapDownloaderTask(ImageView imageView) {?
??????? imageViewReference = new WeakReference<ImageView>(imageView);?
??? }?
??? @Override?
??? protected Bitmap doInBackground(String... params) {?? //實際的下載線程,內部其實是concurrent線程,所以不會阻塞?
???
???????? return downloadBitmap(params[0]);???
? }?
??? @Override?
???? protected void onPostExecute(Bitmap bitmap) {?? //下載完后執行的?
??????? if (isCancelled()) {?
??????????? bitmap = null;?
??????? }?
??????? if (imageViewReference != null) {?
??????????? ImageView imageView = imageViewReference.get();?
??????????? if (imageView != null) {?
??????????????? imageView.setImageBitmap(bitmap);? //下載完設置imageview為剛才下載的bitmap對象?
??????????? }?
??????? }?
??? }?
}
? 方法一、直接通過Android提供的Http類訪問遠程服務器,這里AndroidHttpClient是SDK 2.2中新出的方法,API Level為8,大家需要注意下,靜態訪問可以直接調用,如果SDK版本較低可以考慮Apache的Http庫,當然HttpURLConnection 或URLConnection也可以。?
?? static Bitmap downloadBitmapByCwj(String url) {?
??? final AndroidHttpClient client = AndroidHttpClient.newInstance("Android123");?
??? final HttpGet getRequest = new HttpGet(url);?
??? try {?
??????? HttpResponse response = client.execute(getRequest);?
??????? final int statusCode = response.getStatusLine().getStatusCode();?
??????? if (statusCode != HttpStatus.SC_OK) {??
??????????? Log.e("cwjDebug", "Error " + statusCode + " while retrieving bitmap from " + url);??
??????????? return null;?
??????? }?
?????????
??????? final HttpEntity entity = response.getEntity();?
??????? if (entity != null) {?
??????????? InputStream inputStream = null;?
??????????? try {?
??????????????? inputStream = entity.getContent();??
??????????????? final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);?
??????????????? return bitmap;?
??????????? } finally {?
??????????????? if (inputStream != null) {?
??????????????????? inputStream.close();???
??????????????? }?
??????????????? entity.consumeContent();?
??????????? }?
??????? }?
??? } catch (Exception e) {?
????????? getRequest.abort();?
??????? Log.e("android123Debug", "Error while retrieving bitmap from " + url, e.toString());?
??? } finally {?
??????? if (client != null) {?
??????????? client.close();?
??????? }?
??? }?
??? return null;?
}?
? 這里Android開發網提醒大家,BitmapFactory類的decodeStream方法在網絡超時或較慢的時候無法獲取完整的數據,這里我們通過繼承FilterInputStream類的skip方法來強制實現flush流中的數據,主要原理就是檢查是否到文件末端,告訴http類是否繼續。?
static class FlushedInputStream extends FilterInputStream {?
??? public FlushedInputStream(InputStream inputStream) {?
??????? super(inputStream);?
??? }?
??? @Override?
??? public long skip(long n) throws IOException {?
??????? long totalBytesSkipped = 0L;?
??????? while (totalBytesSkipped < n) {?
??????????? long bytesSkipped = in.skip(n - totalBytesSkipped);?
??????????? if (bytesSkipped == 0L) {?
????????????????? int byte = read();?
????????????????? if (byte < 0) {?
????????????????????? break;? // we reached EOF?
????????????????? } else {?
????????????????????? bytesSkipped = 1; // we read one byte?
????????????????? }?
?????????? }?
??????????? totalBytesSkipped += bytesSkipped;?
??????? }?
??????? return totalBytesSkipped;?
??? }?
}?
? 方法二、AsyncTask異步任務?
? 從Android 1.5固件開始Google提供了一個AsyncTask類來幫助開發者處理異步下載的實現,相對于Thread而言他可以運行在UI線程中,其內部的實現是從Java 5開始的并發包concurrent中派生而來的,總體實現比較可靠就是資源占用略大了些。不過使用起來比簡單。這里下載圖片類 ImageDownloader類的download方法可以很好的處理實現UI顯示等操作,參數一url為遠程server上文件的url,第二個參數為imageview對象,可以直接讓imageview顯示出下載的遠程圖片。?
? public class ImageDownloader {?
??? public void download(String url, ImageView imageView) {?
??????????? BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);?
??????????? task.execute(url);?
??????? }?
??? }?
}?
有關具體的AsyncTask類實現,考慮到圖片可能較大,為了給JVM充分的空間存儲,這里Android123推薦大家使用弱引用來保存ImageView對象。
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {?
??? private String url;?
??? private final WeakReference<ImageView> imageViewReference;? //使用WeakReference解決內存問題?
??? public BitmapDownloaderTask(ImageView imageView) {?
??????? imageViewReference = new WeakReference<ImageView>(imageView);?
??? }?
??? @Override?
??? protected Bitmap doInBackground(String... params) {?? //實際的下載線程,內部其實是concurrent線程,所以不會阻塞?
???
???????? return downloadBitmap(params[0]);???
? }?
??? @Override?
???? protected void onPostExecute(Bitmap bitmap) {?? //下載完后執行的?
??????? if (isCancelled()) {?
??????????? bitmap = null;?
??????? }?
??????? if (imageViewReference != null) {?
??????????? ImageView imageView = imageViewReference.get();?
??????????? if (imageView != null) {?
??????????????? imageView.setImageBitmap(bitmap);? //下載完設置imageview為剛才下載的bitmap對象?
??????????? }?
??????? }?
??? }?
}
總結
以上是生活随笔為你收集整理的android多线程下载图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PendingIntent详解
- 下一篇: Android多线程分析之一:使用Thr