场景:Android,通过inputStream从网络上获取图片

随后两次使用BitmapFactory对InputStream进行操作,一次获取宽高,另一次缩放

但是在缩放时,发现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的解决方法的更多相关文章

  1. Type.GetType()在跨程序集反射时返回null的解决方法

    在开发中,经常会遇到这种情况,在程序集A.dll中需要反射程序集B.dll中的类型.如果使用稍有不慎,就会产生运行时错误.例如使用Type.GetType("BNameSpace.Class ...

  2. Type.GetType()反射另外项目中的类时返回null的解决方法

    项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...

  3. mysql执行load_fle返回NULL的解决方法

    mysql 版本: 5.7.18 问题: 在执行mysql 函数load_file时,该函数将加载指定文件的内容,存储至相应字段.如: SELECT LOAD_FILE("D:\aa.txt ...

  4. Picasso加载网络图片失败,提示decodestream时返回null

    最近遇到一个问题,项目用的图片加载框架是Picasso,网络加载框架是okhttp,项目在加载轮播图时有时可以正常加载,有时,会加载失败,提示decodestream时返回null. 首先,需要确定是 ...

  5. [datatable]关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法

    -- :09关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在实际编程工程中,常常遇到这样的情况:DataTable并不 ...

  6. 【java】Execption的 e.getMessage()为null的解决方法

    ================================ 场景: 当代码出现异常时通常都需要将异常信息写入到日志中,异常信息越详细越有利于问题的排查.而通过的Exception.getMess ...

  7. ajax跨域POST时执行OPTIONS请求服务端返回403forbidden的解决方法

    ajax访问服务端restful api时,由于contentType类型的原因,浏览器会先发送OPTIONS请求. 本人服务端用的是spring mvc框架,web服务器用的是tomcat的,以下给 ...

  8. 安卓BitmapFactory.decodeStream()返回null的问题解决方法

    问题描述: 从网络获取图片,数据为InputStream流对象,然后调用BitmapFactory的decodeStream()方法解码获取图片,返回null. 代码如下: private Bitma ...

  9. .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)

    最近在项目中与别的公司对接业务,对方是Java语言,需要调用对方的WebServices,结果常规的添加web引用的方法可以传过去值,但是返回值为null 查了很多资料,没有解决方法 思考应该是.Ne ...

随机推荐

  1. 利用WM_COPYDATA消息实现进程间通信

    进程间通信最简单的方式就是发送WM_COPYDATA消息,下面通过例子来实现. 发送WM_COPYDATA消息: SendMessage(hRecvWnd, WM_COPYDATA, (WPARAM) ...

  2. 传递任意数量的实参*parameter&使用任意数量的关键字实参**parameter

    1.*形参名(*parameter) 有时候我们不知道知道函数需要接受多少个实参,所以我们可以在形参名前加一个*,是让python创建一个名为parameter的空元组,并将收到的所有值都封装到这个元 ...

  3. jquery表单验证插件 jquery.form.js ------转载

    Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满足日常应用. 1.JQuery框架软件包下载 文件: jquery.rar 大小: 29KB 下载: 下载 2.Form插件下载 文件 ...

  4. wap开发体会<转载>

    前二天因工作需要,上头要求做一个wap版的网站,到网上学习了一天,弄了个beta版出来(http://wap.luckty.com 功能很一般),整理几点经验如下: 1.wap网站用的是wml标识,非 ...

  5. iOS 笔试题

    转:http://blog.sina.com.cn/s/blog_b0c5954101014upb.html 1.截取字符串”20|http://www.621life.com“ 中 ‘|’字符前面及 ...

  6. LAMP集群项目五 部署NFS存储服务并设置WEB服务挂载

    yum install nfs-utils portmap -y 在centos6.5中portmap已经改为rpcbind 先启动rpcbind /etc/init.d/rpcbind start ...

  7. Strut2中的标签

    Struts2的标签用法和示例 1)s:property标签:property 标签用来输出一个值栈属性的值 示例: 输出 Action 属性 customerId 的值: <s:propert ...

  8. debian卸载旧内核

    debian卸载旧内核要先看看有哪些旧的内核,用命令: uname -a dpkg --get-selections |grep linux 如果你的内核是以kernel开头的就把上面的linux改成 ...

  9. Linux 常用资源

    kernel:ftp://kernel.orgcnkernel:http://www.cnkernel.orgoldlinux:http://www.oldlinux.orgminix3:http:/ ...

  10. MongoDB 聚合结果大小限制

    The aggregate command can return either a cursor or store the results in a collection. When returnin ...