[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 ...
随机推荐
- java之基本数据类型与引用数据类型
基本数据类型 需要注意的是字符是基本数据类型,但是字符串不是基本数据类型. 引用数据类型 类.接口类型.数组类型.枚举类型.注解类型. (上面说的字符串String属于引用数据类型中“类”的范畴) 两 ...
- django使用用户名或手机号码登录
django有自己的认证系统,会自动根据用户名和密码进行验证.如果需要使用用户名或手机登录的话,需要重写django的认证后台,并且设置到配置文件中. 重写django的认证后台 class User ...
- jQuery实现,动态自动定位弹窗。JS分页,Ajax请求
工作中碰到一个问题,一个页面中碰到多个地方需要弹窗数据. 网上找了一圈,没有找到合适的,所以自己写了一个. 兼容IE7+,chrome.其它未测试. 需求:点击任意的输入框(也可其它元素,代码中有注释 ...
- hdu 2818(并查集,带权更新)
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【转】Debug 运行正常,Release版本不能正常运行
http://blog.csdn.net/ruifangcui7758/archive/2010/10/18/5948611.aspx引言 如果在您的开发过程中遇到了常见的错误,或许您的Release ...
- 主机批量扫描工具fping,hping安装及使用
https://blog.csdn.net/weixin_39762926/article/details/79476196?utm_source=blogxgwz0 https://blog.csd ...
- .NET对IO的基本操作集合
分享一下对IO的基本使用,很简单的东西,不需要记住,用的时候看一下就可以了. 个人对IO的使用很少,记录日志,保存一些数据,保存文件,其他的基本上很少用到,做商城类的项目应该会用到很多 1.先配置一下 ...
- [水煮 ASP.NET Web API2 方法论](12-3)OData 查询
问题 Web API 怎么支持通用的 OData 系统查询项,例如 $select 或 $filter. 解决方案 为了在 Web API 中启用查询项,我们需要在 Action 上使用 Enable ...
- 转:Super Awesome Fuzzing, Part One
转:https://labsblog.f-secure.com/2017/06/22/super-awesome-fuzzing-part-one/ An informative guide on u ...
- 数学【CF743C】Vladik and fractions
Description 请找出一组合法的解使得\(\frac {1}{x} + \frac{1}{y} + \frac {1}{z} = \frac {2}{n}\)成立 其中\(x,y,z\)为正整 ...