我将http://android-developers.blogspot.gr/2010/07/multithreading-for-performance.html的代码与http://developer.android.com/training/displaying-bitmaps/index结合使用。 html代码。当我尝试从 url 异步下载图像而不更改样本大小时,一切正常。但是当我尝试计算样本大小时,屏幕上没有出现任何内容(gridview)。我阅读了 logcat,发现所有图像都已正确下载。我用于图像下载的代码是下一个:
Bitmap downloadBitmap(String url) {
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();
// get Device Screen Dimensions
DeviceProperties dimensions = new DeviceProperties(activity);
return
decodeBitmapFromResource(url, inputStream,
dimensions.getDeviceWidth(),
dimensions.getDeviceHeight());
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w(TAG, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.w(TAG, "Incorrect URL: " + url);
} catch (Exception e) {
getRequest.abort();
Log.w(TAG, "Error while retrieving bitmap from " + url, e);
} finally {
if ((client instanceof AndroidHttpClient)) {
((AndroidHttpClient) client).close();
}
}
return null;
}
我用于解码的代码是这样的:
public static Bitmap decodeBitmapFromResource(String url, InputStream is,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FlushedInputStream(is), null,
options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options , reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(new FlushedInputStream(is), null,
options);
}
当我同时使用第一个和第二个 BitmapFactory.decodeStream 时出现问题。如果我只使用第二个一切都可以,但实际上我没有制作任何样品。有什么建议吗?我已经浪费了很多时间来寻找它。