BitmapFactory.decodeStream(inputStream) always return null when some bytes are wrong

前端 未结 2 1838
北海茫月
北海茫月 2021-01-06 16:34

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         


        
相关标签:
2条回答
  • 2021-01-06 16:58

    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;
       }
    }
    
    0 讨论(0)
  • 2021-01-06 17:12

    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.

    0 讨论(0)
提交回复
热议问题