我正在编写一个Android应用程序,在那里我从互联网上获取大量图像以显示在图库中。图像首先保存到磁盘,然后由设置为FragmentStatePagerAdapter的ViewPager中的片段提取。
我遇到的问题是,在下载第三个图像之后,之后的任何图像都会导致NullPointerException,其中URL显然为null。这是我使用的downloadBitmap()方法:
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient 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();
final Bitmap bitmap = BitmapFactory
.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or
// IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from "
+ url);
} finally {
if (client != null) {
client.close();
}
}
return null;
}
同样,在下载第三个图像之后,该方法开始吐出错误,指出“从检索位图时出错”而没有将URL附加到末尾,这意味着我将null或空字符串传递给方法。但是,情况并非如此,因为在使用Log.d(“URL”,表示URL)运行方法之前记录我传递的URL字符串表示URL完全有效。 downloadBitmap()代码在以下AsyncTask中调用。
class RetrieveImagesTask extends
AsyncTask<ArrayList<Magazine>, String, ArrayList<Magazine>> {
@Override
protected ArrayList<Magazine> doInBackground(
ArrayList<Magazine>... arg0) {
Bitmap bitmap;
for (Magazine mag : arg0[0]) {
if (!new File(filesDir, mag.ID + ".jpg").exists())
try {
FileOutputStream out = new FileOutputStream(filesDir + mag.ID
+ ".jpg");
Log.d("URL", mag.imageURL);
bitmap = downloadBitmap(mag.imageURL);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
return arg0[0];
}
对这种奇怪的事态有何帮助?可能是内存耗尽问题吗?
答案 0 :(得分:0)
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient 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();
final Bitmap bitmap;
int h = 100; // height in pixels
int w = 100; // width in pixels
if (bitmap != null) {
bitmap = Bitmap.createScaledBitmap(
BitmapFactory
.decodeStream(inputStream), h, w, true);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or
// IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from "
+ url);
} finally {
if (client != null) {
client.close();
}
}
return null;
}