Android實(shí)現(xiàn)多線程下載圖片的方法
很多時(shí)候我們需要在Android設(shè)備上下載遠(yuǎn)程服務(wù)器上的圖片進(jìn)行顯示,今天整理出兩種比較好的方法來(lái)實(shí)現(xiàn)遠(yuǎn)程圖片的下載。
方法一、直接通過(guò)Android提供的Http類(lèi)訪問(wèn)遠(yuǎn)程服務(wù)器,這里AndroidHttpClient是SDK 2.2中新出的方法,API Level為8,大家需要注意下,靜態(tài)訪問(wèn)可以直接調(diào)用,如果SDK版本較低可以考慮Apache的Http庫(kù),當(dāng)然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開(kāi)發(fā)網(wǎng)提醒大家,BitmapFactory類(lèi)的decodeStream方法在網(wǎng)絡(luò)超時(shí)或較慢的時(shí)候無(wú)法獲取完整的數(shù)據(jù),這里我們通過(guò)繼承FilterInputStream類(lèi)的skip方法來(lái)強(qiáng)制實(shí)現(xiàn)flush流中的數(shù)據(jù),主要原理就是檢查是否到文件末端,告訴http類(lèi)是否繼續(xù)。
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異步任務(wù)
從Android 1.5固件開(kāi)始Google提供了一個(gè)AsyncTask類(lèi)來(lái)幫助開(kāi)發(fā)者處理異步下載的實(shí)現(xiàn),相對(duì)于Thread而言他可以運(yùn)行在UI線程中,其內(nèi)部的實(shí)現(xiàn)是從Java 5開(kāi)始的并發(fā)包c(diǎn)oncurrent中派生而來(lái)的,總體實(shí)現(xiàn)比較可靠就是資源占用略大了些。不過(guò)使用起來(lái)比簡(jiǎn)單。這里下載圖片類(lèi) ImageDownloader類(lèi)的download方法可以很好的處理實(shí)現(xiàn)UI顯示等操作,參數(shù)一url為遠(yuǎn)程server上文件的url,第二個(gè)參數(shù)為imageview對(duì)象,可以直接讓imageview顯示出下載的遠(yuǎn)程圖片。
public class ImageDownloader {
public void download(String url, ImageView imageView) {
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
task.execute(url);
}
}
}
有關(guān)具體的AsyncTask類(lèi)實(shí)現(xiàn),考慮到圖片可能較大,為了給JVM充分的空間存儲(chǔ),這里Android123推薦大家使用弱引用來(lái)保存ImageView對(duì)象。
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference; //使用WeakReference解決內(nèi)存問(wèn)題
public BitmapDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) { //實(shí)際的下載線程,內(nèi)部其實(shí)是concurrent線程,所以不會(huì)阻塞
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) { //下載完后執(zhí)行的
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap); //下載完設(shè)置imageview為剛才下載的bitmap對(duì)象
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Dagger2在Android開(kāi)發(fā)中的新用法
本篇文章主要介紹了Dagger2在Android開(kāi)發(fā)中的新用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購(gòu)倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購(gòu)倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Android 中 MD5 的幾種生成方式(小結(jié))
這篇文章主要介紹了Android 中 MD5 的幾種生成方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
OpenGL Shader實(shí)例分析(3)等待標(biāo)識(shí)效果
這篇文章主要介紹了OpenGL Shader實(shí)例分析第3篇,等待標(biāo)識(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見(jiàn)的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的方法。接下來(lái)通過(guò)本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12
Android List刪除重復(fù)數(shù)據(jù)
這篇文章主要介紹了Android List刪除重復(fù)數(shù)據(jù)的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06

