首页 文章 精选 留言 我的

精选列表

搜索[对象],共10000篇文章
优秀的个人博客,低调大师

[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 本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/3946659.html如需转载请自行联系原作者 demoblog

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码