BitmapFactory.decodeStream(inputStream)返回null的解决方法
场景:Android,通过inputStream从网络上获取图片
随后两次使用BitmapFactory对InputStream进行操作,一次获取宽高,另一次缩放
但是在缩放时,发现inputStream不能用了
The problem was that once you've used an InputStream from a HttpUrlConnection, you can't rewind and use the same InputStream again.
解决办法就是把inputStream里面的数据提取出来,针对数据进行操作就好了
tips:16384是每次迭代从InputStream里面读取的数据量的,是2的N次方,作者使用这个数字是出于效率考虑的
16384 is a fairly arbitrary choice although I tend to favour powers of 2 to increase the chance of the array aligning with word boundaries. pihentagy's answer shows how you can avoid using an intermediate buffer, but rather allocate an array of the correct size. Unless you're dealing with large files I personally prefer the code above, which is more elegant and can be used for InputStreams where the number of bytes to read is not known in advance
public static byte[] streamToByte(InputStream is) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
try {
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buffer.toByteArray();
}
BitmapFactory.decodeStream(inputStream)返回null的解决方法的更多相关文章
- Type.GetType()在跨程序集反射时返回null的解决方法
在开发中,经常会遇到这种情况,在程序集A.dll中需要反射程序集B.dll中的类型.如果使用稍有不慎,就会产生运行时错误.例如使用Type.GetType("BNameSpace.Class ...
- Type.GetType()反射另外项目中的类时返回null的解决方法
项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...
- mysql执行load_fle返回NULL的解决方法
mysql 版本: 5.7.18 问题: 在执行mysql 函数load_file时,该函数将加载指定文件的内容,存储至相应字段.如: SELECT LOAD_FILE("D:\aa.txt ...
- Picasso加载网络图片失败,提示decodestream时返回null
最近遇到一个问题,项目用的图片加载框架是Picasso,网络加载框架是okhttp,项目在加载轮播图时有时可以正常加载,有时,会加载失败,提示decodestream时返回null. 首先,需要确定是 ...
- [datatable]关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
-- :09关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在实际编程工程中,常常遇到这样的情况:DataTable并不 ...
- 【java】Execption的 e.getMessage()为null的解决方法
================================ 场景: 当代码出现异常时通常都需要将异常信息写入到日志中,异常信息越详细越有利于问题的排查.而通过的Exception.getMess ...
- ajax跨域POST时执行OPTIONS请求服务端返回403forbidden的解决方法
ajax访问服务端restful api时,由于contentType类型的原因,浏览器会先发送OPTIONS请求. 本人服务端用的是spring mvc框架,web服务器用的是tomcat的,以下给 ...
- 安卓BitmapFactory.decodeStream()返回null的问题解决方法
问题描述: 从网络获取图片,数据为InputStream流对象,然后调用BitmapFactory的decodeStream()方法解码获取图片,返回null. 代码如下: private Bitma ...
- .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
最近在项目中与别的公司对接业务,对方是Java语言,需要调用对方的WebServices,结果常规的添加web引用的方法可以传过去值,但是返回值为null 查了很多资料,没有解决方法 思考应该是.Ne ...
随机推荐
- mysql更改utf8编码方式
方法1: 一.查看数据库编码格式 1 mysql> show variables like 'character_set_database'; 二.查看数据表的编码格式 1 mysql> ...
- 在scrollview中双击定点放大的代码
双击放大是 iPhone 的一个基本操作,第三方程序里引入这一功能的话,主要是在 scrollview 呈现一张图片或者 PDF 页面时,双击可以放大,主要代码如下 - (void)scrollVie ...
- HYSBZ 2243(染色)
题目链接:传送门 题目大意:中文题,略 题目思路:树链剖分,区间更新,区间查询. 闲谈: 只想说这道题做的好苦逼..去长春现场赛之前就没A,回来后又做了2天才A掉,蒟蒻太菜了 这道题也没有想 ...
- 【黑金原创教程】【Modelsim】【第一章】Modelsim仿真的扫盲文
声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...
- 基于k8s的promethus监控
没有监控 就没有眼睛. 除了k8s的基本监控外(pod运行状况.占用内存.cpu).为了对微服务项目中的(1)各种参数线程池.QPS.RT.业务指标(2)系统负载.thread.mem.class.t ...
- 学习ASP.NET MVC3(6)----- Filte
前言 在开发大项目的时候总会有相关的AOP面向切面编程的组件,而MVC(特指:Asp.Net MVC,以下皆同)项目中不想让MVC开发人员去关心和写类似身份验证,日志,异常,行为截取等这部分重复的代码 ...
- quartz启动Quartz : org.quartz.SchedulerConfigException: Thread count must be > 0
检查quartz.properties数据源配置是否正常
- JSTL-标准标签库
JSTL1.2中的标签库可以分成5类区域 核心:uri="http://java.sun.com/jsp/jstl/core" prefix=“c” XML: ...
- centos7常用命令集合
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] CentOS7 常用命令集合 这两天一直在对CentOS 7.2进行初体验,各种学习命令肿么用,不过其实大多和DOS是一 ...
- jquery页面滚动显示浮动菜单栏锚点定位效果
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...