C++盤算每一個字符湧現的次數。本站提示廣大學習愛好者:(C++盤算每一個字符湧現的次數)文章只能為提供參考,不一定能成為您想要的結果。以下是C++盤算每一個字符湧現的次數正文
許多時刻我們須要在Android裝備高低載長途辦事器上的圖片停止顯示,明天整頓出兩種比擬好的辦法來完成長途圖片的下載。
辦法1、直接經由過程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;
}
}
辦法2、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對象
}
}
}
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。