[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
相关文章
|
4月前
|
Android开发 开发者
Android自定义view之利用drawArc方法实现动态效果
本文介绍了如何通过Android自定义View实现动态效果,重点使用`drawArc`方法完成圆弧动画。首先通过`onSizeChanged`进行测量,初始化画笔属性,设置圆弧相关参数。核心思路是不断改变圆弧扫过角度`sweepAngle`,并调用`invalidate()`刷新View以实现动态旋转效果。最后附上完整代码与效果图,帮助开发者快速理解并实践这一动画实现方式。
121 0
|
2月前
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
134 11
|
3月前
|
JavaScript Java API
ArkUI-X与Android桥接通信之方法回调
本文介绍了ArkUI与Android平台之间的消息传递机制,涵盖双向数据交互、方法调用及不同数据类型的处理。主要内容包括:1) ArkUI调用Android方法,通过`callMethod`实现;2) Android调用ArkUI方法,借助`registerMethod`注册并调用;3) ArkUI监听Android方法,支持方法注册与注销;4) 带回调的跨平台方法调用,支持无参与有参场景;5) 不同数据类型(如字符串、数组、对象)的传递示例。代码示例展示了TypeScript和Java的实现细节,帮助开发者理解桥接机制的具体应用。
|
11月前
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
352 15
Android 系统缓存扫描与清理方法分析
|
12月前
|
存储 缓存 编解码
Android经典面试题之图片Bitmap怎么做优化
本文介绍了图片相关的内存优化方法,包括分辨率适配、图片压缩与缓存。文中详细讲解了如何根据不同分辨率放置图片资源,避免图片拉伸变形;并通过示例代码展示了使用`BitmapFactory.Options`进行图片压缩的具体步骤。此外,还介绍了Glide等第三方库如何利用LRU算法实现高效图片缓存。
161 20
Android经典面试题之图片Bitmap怎么做优化
|
12月前
|
ARouter 测试技术 API
Android经典面试题之组件化原理、优缺点、实现方法?
本文介绍了组件化在Android开发中的应用,详细阐述了其原理、优缺点及实现方式,包括模块化、接口编程、依赖注入、路由机制等内容,并提供了具体代码示例。
218 2
|
开发工具 uml git
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
本文分享了下载AOSP源码的方法,包括如何使用repo工具和处理常见的repo sync错误,以及配置Python环境以确保顺利同步特定版本的AOSP代码。
1874 0
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
|
XML Android开发 数据格式
[Android]Bitmap Drawable
[Android]Bitmap Drawable
111 0
|
Android开发
安卓根据需要压缩图片大小bitmap,drawable
安卓根据需要压缩图片大小bitmap,drawable
259 0
|
XML 存储 缓存
安卓 Bitmap 和 Drawable 的使用
Bitmap 的使用 高效加载大位图 解码大的 bitmap,然后加载一个较小的图片到内存中去,从而避免超出程序的内存限制。
318 0
 安卓 Bitmap 和 Drawable 的使用

热门文章

最新文章