1

我正在编写一个 Android 应用程序,我从互联网上获取一些图像以显示在画廊中。图像在被设置为 FragmentStatePagerAdapter 的 ViewPager 中的片段获取之前首先保存到磁盘。

我遇到的问题是,在下载第三张图片后,之后的任何图片都会导致 NullPointerException,其中 URL 显然为空。这是我使用的 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,这意味着我正在将空字符串或空白字符串传递给该方法。但是,情况并非如此,因为在使用 Log.d("URL", saidURL) 运行方法之前记录我传递的 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];
    }

对这种奇怪的事态有什么帮助吗?可能是内存不足的问题吗?

4

1 回答 1

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;

}

于 2013-02-12T06:32:38.140 回答