我有一个班级" ImageDownloader"专为列表视图创建。它获取了imageView和url,并在url上设置了这个imageView位图。我使用AsyncTask在单独的线程中下载了位图:
class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewWeakReference;
// Getters
public String getUrl() {
return url;
}
// Creator
public ImageLoadTask(String url, ImageView imageView) {
this.url = url;
imageViewWeakReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
InputStream input = new URL(url).openStream();
Bitmap finalBitmap = BitmapFactory.decodeStream(input);
return finalBitmap;
} catch (Exception e) {
Log.d("TAG", "exception in doInBackground " + url);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
if (isCancelled()) {
result = null;
Log.d("TAG","was cancelled "+url);
}
if (imageViewWeakReference != null && result != null) {
ImageView imageView = imageViewWeakReference.get();
final ImageLoadTask imageLoadTask =
getImageLoadTask(imageView);
if (this == imageLoadTask ) {
imageView.setImageBitmap(result);
}
}
}
}
我有方法可以使用方法imageLoadTask.cancel(true)
取消之前的下载(如果需要(对于testle imageView有两个imageLoadTasks,我只需要最后一个)。
一切正常(应用程序正常并正确显示位图),但当我开始应用时,我得到异常
04-10 00:42:08.043 583-645/leopikinc.musiciansyandex D/TAG﹕ exception in doInBackground http://avatars.yandex.net/get-music-content/dfc531f5.p.1080505/300x300
04-10 00:42:08.043 583-645/leopikinc.musiciansyandex W/System.err﹕ java.io.InterruptedIOException: thread interrupted
04-10 00:42:08.044 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.Timeout.throwIfReached(Timeout.java:145)
04-10 00:42:08.044 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.Okio$1.write(Okio.java:73)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:902)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
04-10 00:42:08.045 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at java.net.URL.openStream(URL.java:470)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at leopikinc.musiciansyandex.ImageDownloader$ImageLoadTask.doInBackground(ImageDownloader.java:92)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at leopikinc.musiciansyandex.ImageDownloader$ImageLoadTask.doInBackground(ImageDownloader.java:73)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-10 00:42:08.046 583-645/leopikinc.musiciansyandex W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-10 00:42:08.047 583-645/leopikinc.musiciansyandex W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-10 00:42:08.047 583-645/leopikinc.musiciansyandex W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-10 00:42:08.047 583-645/leopikinc.musiciansyandex W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
04-10 00:42:08.347 583-648/leopikinc.musiciansyandex W/System.err﹕ java.io.InterruptedIOException: thread interrupted
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.Timeout.throwIfReached(Timeout.java:145)
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.Okio$2.read(Okio.java:132)
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:349)
04-10 00:42:08.349 583-648/leopikinc.musiciansyandex W/System.err﹕ at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-10 00:42:08.350 583-648/leopikinc.musiciansyandex W/System.err﹕ at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
04-10 00:42:08.350 583-648/leopikinc.musiciansyandex W/System.err﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at leopikinc.musiciansyandex.ImageDownloader$ImageLoadTask.doInBackground(ImageDownloader.java:93)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at leopikinc.musiciansyandex.ImageDownloader$ImageLoadTask.doInBackground(ImageDownloader.java:73)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-10 00:42:08.358 583-648/leopikinc.musiciansyandex W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-10 00:42:08.359 583-648/leopikinc.musiciansyandex W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
04-10 00:42:08.359 583-648/leopikinc.musiciansyandex D/skia﹕ ---- read threw an exception
04-10 00:42:08.360 583-648/leopikinc.musiciansyandex D/skia﹕ --- decoder->decode returned false
滚动ListView时,我有时会遇到此异常。
当我尝试使用HttpClient等弃用方法下载位图时,没有例外。这是没有例外的代码
Bitmap downloadBitmap(String url) {
// AndroidHttpClient is not allowed to be used from the main thread
final HttpClient client = AndroidHttpClient.newInstance("Android");
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
return BitmapFactory.decodeStream(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.w(LOG_TAG, "Incorrect URL: " + url);
} catch (Exception e) {
getRequest.abort();
Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
} finally {
if ((client instanceof AndroidHttpClient)) {
((AndroidHttpClient) client).close();
}
}
return null;
}
我在downloadBitmap
doInBackgroud
protected Bitmap doInBackground(String... params) {
url = params[0];
return downloadBitmap(url);
}