[Android Pro] 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
在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对象的解决方法的更多相关文章
- 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
在android sdk 1.6版本API帮助文档中,其中关于BitmapFactory.decodeFactory.decodeStream(InputStream is)的帮助文档是这么说明的: ...
- Android 开发之异常处理篇(一):SDK Manager 闪退的解决方法
这个问题困扰了我很久,之前没解决,就放一放.后来我又专门拿了一个下午来找解决方法,终于搞定! 我的解决方法是修改 android.bat,直接指定java.exe所在位置,不用去调用find_java ...
- android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下 布局文件中的TextView属性 复制代码代码如下: < ...
- AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到
AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到
- 使用Android Studio过程中,停留在“Building ‘工程名’ Gradle project info”的解决方法
http://www.loverobots.cn/in-the-process-of-using-studio-android-the-solution-of-the-project-info-gra ...
- Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法
前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的 ...
- 【android】关于自己实现adapter后gridview中item无法被选中的解决方法
有时候,自己继承实现了baseadapter将其赋给gridview之后,gridview会十分奇怪的无法选中内部的item. 经过仔细研究,我发现是在继承的时候多复写了几个方法,解决方法就是,只保留 ...
- eclipse中的出现在打包一次后,后面新建的项目都出错了,出现support_v7下面出现红线及解决方法及为什么eclipse中项目继承ActionBarActivity解决方法一样
第一次写博客,有什么问题或者想法的希望各位可以进行评论交流,望大家多多包涵! 遇到的问题是在新建的项目都出错了,出现support_v7下面出现红线及解决方法及为什么eclipse中项目继承Actio ...
- WIN7+IE8环境QTP11不能录制和识别web对象的解决方法
在项目稍微空闲的时间,在办公电脑上面装上QTP11来学习.但是发现在录制脚本时无法录制web对象,在网上找解决方法说以管理员的身份运行QTP就可以解决无法录制的问题,用这方法证明是ok的.后来用Obj ...
随机推荐
- Nginx-1.6.3反向代理
源码安装nginx cat /etc/redhat-release uname -rm yum install pcre-devel openssl-devel -y rpm -qa pcre pcr ...
- debian下没有公钥解决办法
debian下没有公钥解决办法 执行命令:apt-get update 出现如下错误 正在读取软件包列表... 完成 W: 以下 ID 的密钥没有可用的公钥: 8B48AD6246925 ...
- hihocoder 1145 : 幻想乡的日常
#1145 : 幻想乡的日常 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 幻想乡一共有n处居所,编号从1到n.这些居所被n-1条边连起来,形成了一个树形的结构. 每处 ...
- redis之(六)redis的列表类型的命令
[一]向列表两端添加元素 -->命令:LPUSH key value [value ...] -->向列表的左侧添加元素,返回值表示增加元素后列表的长度 -->命令:RPUSH ke ...
- Set Matrix Zeroes——常数空间内完成
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Did yo ...
- 解决报错:Access denied for user 'root'@'xxx' ,MySQL不允许从远程访问的方法 .
原文地址:传送门 例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDEN ...
- react native android 应用状态(前端或后台)的判断
当Android应用程序被暂时放到了后台,或者又重新回到前台,是否有相应的事件可以处理到? 例如,当你的应用暂时放到了后台,是否应该做出一些操作,暂时保存界面上的数据? 可以参考:https://gi ...
- Python和xml简介
python提供越来越多的技术来支持xml,本文旨在面向初学利用Python处理xml的读者,以教程的形式介绍一些基本的xml出来概念.前提是读者必须知道一些xml常用术语. 先决条件 本文所有的例子 ...
- mysql查询优化以及面试小结
mysql面试小结: 1.mysql的基本架构 2.mysql的索引 btree+的原理 3.mysql的索引优化 4.mysql的sql查询优化 慢查询日志 Show prodile 全局查询日志 ...
- 【JAVA】在线程里使用线程外的变量为什么一定要是final类型
这个情况真的碰到很多,开始的时候也很难理解,但是既然IDE提示要final那我就final咯,跑通就行管那么多呢.然而这并不是科学的学习方法,万一面试问你呢那不是倒了大霉. OK,看了一些