java - 当某些字节错误时,BitmapFactory.decodeStream(inputStream) 总是返回 null

标签 java android bitmap byte inputstream

我正在构建一个 Android 应用程序,但我目前无法从 URL 检索位图。这是我正在使用的代码:

public static Bitmap bmpFromURL(URL imageURL){

    Bitmap result = null;

    try {

        HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();

        connection.setDoInput(true);

        connection.connect();

        InputStream input = connection.getInputStream();

        result = BitmapFactory.decodeStream(input);


    } catch (IOException e) {


        e.printStackTrace();

    }

    return result;

}

写入图片时一切正常,但当某些字节错误时,结果为空。我认为这基本上是可以预料的,因为它写在 BitmapFactory.decodeStream 的文档中:

If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.

问题是,我的网络浏览器可以很好地解释我的错误图片,我可以在 iPhone 平台上这样做。

有没有办法忽略那些错误的像素?也许有选项参数?

感谢任何帮助

罗曼

最佳答案

这是一个已知错误,已在未来的 Android 版本中修复。您可以通过首先将 InputStream 的内容复制到 byte[] 数组然后解码字节数组本身来解决它。

关于java - 当某些字节错误时,BitmapFactory.decodeStream(inputStream) 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3601820/

相关文章:

java - 简单的 if 语句似乎没有执行代码的某些部分

java - 打包运行 Hadoop MapReduce 应用程序所需的所有 jar 文件的好方法吗?

java - 将图像文件从 RGB 转换为 YUV 的有效方法

android - Account Kit在短信登录流程中没有 'pre-fill the user'的电话号码'

java - 使用 get 时 LRUCache 条目重新排序

android - 适用于 Android 的类似 Excel 的垂直和水平标题

android - 在 CoordinatorLayout 中使用时,页脚会滚出屏幕

尝试模糊位图时出现android空指针异常

java - 位图未显示在对话框中

android - 如何设置壁纸使用应用程序服务壁纸和联系人照片?