在android sdk 1.6版本API帮助文档中,其中关于BitmapFactory.decodeFactory.decodeStream(InputStream is)的帮助文档是这么说明的:

Bitmap android.graphics.BitmapFactory.decodeStream(InputStream is)

public static Bitmap decodeStream (InputStream is)
Since: API Level 1
Decode an input stream into a bitmap. <strong>If the input stream is null, or cannot be used to decode a bitmap, the function returns null</strong>. The stream's position will be where ever it was after the encoded data was read. Parameters
is The input stream that holds the raw data to be decoded into a bitmap. Returns
The decoded bitmap, or null if the image data could not be decoded.
public static Bitmap bmpFromURL(URL imageURL){
Bitmap result = null;
try {
HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
result = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

后来调试发现,result为null,加之查看帮助文档中的黑体字,
所以在所获得的InputStream不为空的情况下,调用BitmapFactory.decodeStream(is)方法,他也有可能无法解码成bitmap,刚开始我怀疑是本身图片地址有问题,或图片自身格式不正确,但通过浏览器查看,图片显示正常,而且,我是保存了几十张图片,但每次都会有个别几张图片无法正常显示,需要重复下载三四次,才可能保存成功。

后来在一篇文章中才发现,原来这是android 1.6版本的一个bug!

有牛人提出的一个解决办法,我试了试,问题解决了

首先在原方法中改一句:

result = BitmapFactory.decodeStream(new PatchInputStream(input));

再创建一个类:

public class PatchInputStream extends FilterInputStream{

        protected PatchInputStream(InputStream in) {
super(in);
// TODO Auto-generated constructor stub
} public long skip(long n)throws IOException{
long m=0l;
while(m<n){
long _m=in.skip(n-m);
if(_m==0l){
break;
}
m+=_m;
}
return m;
}
}

第二种方法:最终用的是这种方法

InputStream is = httpConn.getInputStream();

if (is == null){
       throw new RuntimeException("stream is null");
  }else{
       try {
           byte[] data=readStream(is);
           if(data!=null){
           bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
       }
  } catch (Exception e) {
       e.printStackTrace();
  }
   is.close();
  }

    /*
* 得到图片字节流 数组大小
* */
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}

[Android Pro] 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法的更多相关文章

  1. 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法

    在android sdk 1.6版本API帮助文档中,其中关于BitmapFactory.decodeFactory.decodeStream(InputStream is)的帮助文档是这么说明的: ...

  2. Android 开发之异常处理篇(一):SDK Manager 闪退的解决方法

    这个问题困扰了我很久,之前没解决,就放一放.后来我又专门拿了一个下午来找解决方法,终于搞定! 我的解决方法是修改 android.bat,直接指定java.exe所在位置,不用去调用find_java ...

  3. android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法

    这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下 布局文件中的TextView属性 复制代码代码如下: < ...

  4. AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到

    AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到

  5. 使用Android Studio过程中,停留在“Building ‘工程名’ Gradle project info”的解决方法

    http://www.loverobots.cn/in-the-process-of-using-studio-android-the-solution-of-the-project-info-gra ...

  6. Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法

    前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的 ...

  7. 【android】关于自己实现adapter后gridview中item无法被选中的解决方法

    有时候,自己继承实现了baseadapter将其赋给gridview之后,gridview会十分奇怪的无法选中内部的item. 经过仔细研究,我发现是在继承的时候多复写了几个方法,解决方法就是,只保留 ...

  8. eclipse中的出现在打包一次后,后面新建的项目都出错了,出现support_v7下面出现红线及解决方法及为什么eclipse中项目继承ActionBarActivity解决方法一样

    第一次写博客,有什么问题或者想法的希望各位可以进行评论交流,望大家多多包涵! 遇到的问题是在新建的项目都出错了,出现support_v7下面出现红线及解决方法及为什么eclipse中项目继承Actio ...

  9. WIN7+IE8环境QTP11不能录制和识别web对象的解决方法

    在项目稍微空闲的时间,在办公电脑上面装上QTP11来学习.但是发现在录制脚本时无法录制web对象,在网上找解决方法说以管理员的身份运行QTP就可以解决无法录制的问题,用这方法证明是ok的.后来用Obj ...

随机推荐

  1. js对页面对float类型的数据格式化处理

    <script>        document.write(parseFloat(<s:property value="assureterm"/>)); ...

  2. Vim常用命令(转)—默写版

    1.光标移动 上: 下: 左: 『字母小写』 右: 上一行行首: 『减号』 下一行行首: 行首: 『数字0』 行尾: 单词词尾或后一个单词词尾: 后一个单词词首: 单词词首或前一个单词词首: 跳转到特 ...

  3. [路由] -- Yii2 url地址美化与重写

    转载:http://blog.csdn.net/lmjy102/article/details/53857520

  4. linux系统下部署war包

    http://blog.csdn.net/hellowangchaochao/article/details/73223773

  5. 常用SQL时间格式SQLServer中文版的默认的日期字段datetime格式是yyyy-mm-d

    常用SQL时间格式 SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm 例如: select getdate() -- ::08.177 ...

  6. ibatis中的符号#跟$区别

    昨天一个项目中在写ibatis中的sql语句时,order by #field#, 运行时总是报错,后来上网查了查,才知道这里不该用#,而应该用$,随即查了下#与$的区别.  总结如下:  1.#是把 ...

  7. html5 -audio-移动端如何自动播放

    最近在做一些活动类页面或者类似于易企秀类型的轻应用经常遇到关于audio标签的应用,对于audio相关的常用知识点以及一些相关的问题如下: <audio id="audios" ...

  8. 【JBPM4】判断节点decision 方法2 condition

    运用condition计算并判断. JPDL <?xml version="1.0" encoding="UTF-8"?> <process ...

  9. CSS优先级问题以及jQuery中的.eq()遍历方法和:eq()选择器的差别

    在写一个TAB选项卡的时候遇到几个有意思的问题,记录下来 先把代码贴出来 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  10. Mac安装Maven

    1.从官网(https://maven.apache.org/download.cgi)下载 Maven 并解压. 2.配置环境 .  vim ~/.bash_profile export MAVEN ...