场景: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. HDU 1058 Humble Numbers (动规+寻找丑数问题)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  2. Spring_day04--SSH框架整合过程

    SSH框架整合过程 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭建hi ...

  3. mysql触发器小实验

    今天实验了一下mysql的触发器 mysql> use test; Database changed mysql> desc time; +-------+---------------- ...

  4. Linux命令之乐--test

    官方文档: help test File operators: -a FILE True if file exists. -b FILE True if file is block special. ...

  5. Windows电脑键盘快捷键大全【最全的快捷键】

    Windows电脑键盘快捷键大全[最全的快捷键] 一.常见用法: F1显示当前程序或者windows的帮助内容. F2当你选中一个文件的话,这意味着“重命名” F3当你在桌面上的时候是打开“查找:所有 ...

  6. 动态svg效果

    import React from 'react'; import TweenOne from 'rc-tween-one'; import SvgDrawPlugin from 'rc-tween- ...

  7. Struts2 取消 下载时异常

    Struts2环境下,通过Struts2提供的下载方式进行下载时出现的java.lang.IllegalStateException异常 2011-1-820:34:20 org.apache.cat ...

  8. Python--Get and Post

    #python3 get and post 简单封装 from urllib import request, parse import json def RequestMethod(methodR, ...

  9. Python全栈day13(作业讲解字典嵌套实现用户输入地址信息添加及查看)

    要求: 列出字典对应节点名称,根据用户输入可以添加节点,查看节点等功能,这里以地址省-市-县等作为列子,此题熟悉字典嵌套功能 vim day13-16.py db = {} path = [] whi ...

  10. C#批量入库

    public static void BulkCopyToDB(DataTable dt, string conn, string tableName, out string msg) { msg = ...