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

发布于 2024-09-17 10:29:50 字数 817 浏览 2 评论 0原文

我正在构建一个 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 的文档中写的:

如果输入流为 null,或者无法用于解码位图,则该函数返回 null。流的位置将是读取编码数据后的位置。

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

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

任何帮助感谢

罗曼

I'm building an android app and I'm currently having trouble retrieving a bitmap from an URL. Here is the code I'm using :

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;

}

Everything works fine when the picture's write but when some bytes are wrong, result gets null. I think it's basically expectable as it's written this in the doc of 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.

The problem is, my wrong picture is well interpreted by my web browser and I can do so on iPhone platform.

Is there a way to sort of ignore those wrong pixels? maybe with the option parameter?

Any help appreciated

Romain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘寄 2024-09-24 10:29:50

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

This is a known bug fixed in a future version of Android. You can work around it by first copying the content of the InputStream into a byte[] array and then decoding the byte array itself.

撩起发的微风 2024-09-24 10:29:50

试试这个..它对我有用。希望有帮助。

result = BitmapFactory.decodeStream(new PatchInputStream(input));

public class PatchInputStream extends FilterInputStream {
   public PatchInputStream(InputStream input) {
       super(input);
   }
   public long skip(long n) throws IOException {
       long m = 0L;
       while (m < n) {
           long _m = in.skip(n-m);
           if (_m == 0L) break;
           m += _m;
       }
       return m;
   }
}

Try this one out..It worked for me. hope it helps.

result = BitmapFactory.decodeStream(new PatchInputStream(input));

public class PatchInputStream extends FilterInputStream {
   public PatchInputStream(InputStream input) {
       super(input);
   }
   public long skip(long n) throws IOException {
       long m = 0L;
       while (m < n) {
           long _m = in.skip(n-m);
           if (_m == 0L) break;
           m += _m;
       }
       return m;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文