BitmapFactory.decodeStream。报错:SkImageDecoder::Factory returned null 原因及解决方法

最近在写一个相机调用相册的图片,显示到桌面的View上,BitmapFactory.decodeStream 调用了 结果报错了SkImageDecoder::Factory returned nul。

废话不多说,来上我自己的代码。

if(resultCode==RESULT_OK){
				try {
					Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
					
					picture.setImageBitmap(bitmap);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}

看了很多博客,还是不太明白,看了源代码好像明白了。

下面是BitmapFactory.decodeStream 的SDK源代码

 public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) {
        // we don't throw in this case, thus allowing the caller to only check
        // the cache, and not force the image to be decoded.
        if (is == null) {
            return null;
        }

        // we need mark/reset to work properly

        if (!is.markSupported()) {
            is = new BufferedInputStream(is, 16 * 1024);
        }

        // so we can call reset() if a given codec gives up after reading up to
        // this many bytes. FIXME: need to find out from the codecs what this
        // value should be.
        is.mark(1024);

用中文解释下

1、当is这个inputstream为空时,我们不抛出异常。因此仅允许调用者检查缓存,不强制把图片解码。

2、我们需要标记和重置来让解析图片这个工作更恰当

3、所以,当读取的字节到1024(1kb)时,解码器停止解码,之后我们调用重置方法。


注:is.mark(1024),这个方法是写死的。即当图片超过1kb,我们解析到1kb的时候,然后重置,就会抛出异常。


解决方法:使用BitmapFactory.decodebyteArrays 或者 BitmapFactory.Options





你可能感兴趣的:(BitmapFactory.decodeStream。报错:SkImageDecoder::Factory returned null 原因及解决方法)