【问题标题】:How to fix an image downloading/saving error on Android如何修复 Android 上的图像下载/保存错误
【发布时间】:2013-01-27 06:39:16
【问题描述】:

我正在编写一个 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];
    }

对这种奇怪的情况有什么帮助吗?会不会是内存不足的问题?

【问题讨论】:

  • 能否通过将catch部分更改为e.printStackTrace()来发布异常详细信息。
  • 原来我从中获取 URL 的服务器有一半时间给我提供了空 URL 字符串。

标签: android url bitmap android-asynctask inputstream


【解决方案1】:
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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-11
    • 2019-06-13
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多