SkImageDecoder::Factory returned null
2015-09-10 20:23
399 查看
最近在做大图片的加载,途中遇到这样一个问题: 图片在压缩文件中,我先用BitmapFactory取图片尺寸,计算之后再按照合适尺寸取出Bitmap,代码如下:
图片死活就是显示不出来,log中
网上搜索了好多篇文章,都说的含糊其辞,于是看了SDK源代码: 在BitmapFactory.decodeStream中
于是看明白了,第一次取图片尺寸的时候is这个InputStream被使用过了,再真正取图片的时候又使用了这个InputStream,此时流的起始位置已经被移动过了,需要调用is.reset()来重置,然后再decodeStream(imgInputStream, null, options)就没问题了。 但是注意一个问题,is.mark(1024)是SDK中写死的,如果图片的大小超过1024字节,第一次decode取尺寸之后调用is.reset()会抛出IOException,所以建议使用BitmapFactory的其他decode方法,如果是网络读过来的流,最好在本地存成文件缓存,然后通过decodeFileDescriptor方法就没这种问题了。
我解决的方法是 2次打开流 一个用htttpClient 一个用HttpURLConnection
关键代码:
//... options.inJustDecodeBounds = true; BitmapFactory.decodeStream(imgInputStream, null, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(imgInputStream, null, options); //...
图片死活就是显示不出来,log中
SkImageDecoder::Factory returned null
网上搜索了好多篇文章,都说的含糊其辞,于是看了SDK源代码: 在BitmapFactory.decodeStream中
// we need mark/reset to work properly if (!is.markSupported()) { is = new BufferedInputStream(is, DECODE_BUFFER_SIZE); } // 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); //...
于是看明白了,第一次取图片尺寸的时候is这个InputStream被使用过了,再真正取图片的时候又使用了这个InputStream,此时流的起始位置已经被移动过了,需要调用is.reset()来重置,然后再decodeStream(imgInputStream, null, options)就没问题了。 但是注意一个问题,is.mark(1024)是SDK中写死的,如果图片的大小超过1024字节,第一次decode取尺寸之后调用is.reset()会抛出IOException,所以建议使用BitmapFactory的其他decode方法,如果是网络读过来的流,最好在本地存成文件缓存,然后通过decodeFileDescriptor方法就没这种问题了。
我解决的方法是 2次打开流 一个用htttpClient 一个用HttpURLConnection
关键代码:
HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(imageUrl); HttpResponse response = client.execute(httpGet); int code = response.getStatusLine().getStatusCode();
InputStream is = response.getEntity().getContent();
相关文章推荐
- 基本概念
- HTML 简介
- Mac下,在myeclipse中导入jdk和javaEE
- LPC43xx SGPIO Configuration -- Why not use GPDMA ?
- uname -r和uname -a了解
- POJ 1840 Brainman(逆序对数)
- Effective C++ 条款36 绝不重新定义继承而来的non-virtual函数
- BGP-RR 路由反射器工作原理 推荐
- Android将背景切换致白色的。(老是忘记...
- 《神探tcpdump第二招》-linux命令五分钟系列之三十六
- maven利用Profile构建不同环境的部署包
- 剑指Offer系列---(17)调整数组顺序使奇数位于偶数前面
- Java 堆和栈的区别
- 《神探tcpdump第一招》-linux命令五分钟系列之三十五
- 加快mydumper与myloader导出导入
- IOS开发-琐碎的知识点总结1
- 《dig挖出DNS的秘密》-linux命令五分钟系列之三十四
- CentOS7中宿主机配置多网卡绑定和建桥的方法
- Javascript学习笔记
- MEAN:Express相关资料