zoukankan      html  css  js  c++  java
  • 【Android】 Bitmap出现 decoder>decode returned false 错误

    Android Bitmap出现 decoder->decode returned false 错误

    在开发Android中广告条,实现多张广告图片切换,遇到广告图片下载失败,抓取LOG

    出现的问题为 decoder->decode returned false。

    上网搜索发现解决办法 ,

    参考文章 Android decoder->decode returned false for Bitmap download  

    文章地址 http://blog.163.com/dangzhengtao@yeah/blog/static/77800874201121814455655/

    一种方法是

    1.创建静态类FlushedInputStream

    static class FlushedInputStream extends FilterInputStream {
            public FlushedInputStream(InputStream inputStream) {
                super(inputStream);
            }
     
            @Override
            public long skip(long n) throws IOException {
                long totalBytesSkipped = 0L;
                while (totalBytesSkipped < n) {
                    long bytesSkipped = in.skip(n - totalBytesSkipped);
                    if (bytesSkipped == 0L) {
                        int b = read();
                        if (b < 0) {
                            break;  // we reached EOF
                        } else {
                            bytesSkipped = 1; // we read one byte
                        }
                    }
                    totalBytesSkipped += bytesSkipped;
                }
                return totalBytesSkipped;
            }
        }

    2. 修改图片下载代码

    public void setDrawable() {
            URL url = null;
            URLConnection conn = null;
            try {
                url = new URL(“图片地址”);
                conn = url.openConnection();
                Bitmap b = BitmapFactory.decodeStream(new FlushedInputStream(conn.getInputStream()));
                this.drawable = new BitmapDrawable(b);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                url = null;
                conn = null;
            }
        }

    另一种方法是

    这里直接帖代码了,没有验证,有兴趣的朋友可以研究。

    public static Bitmap loadImageFromUrl(String url) {
            URL m;
            InputStream i = null;
            BufferedInputStream bis = null;
            ByteArrayOutputStream out =null;
            try {
                m = new URL(url);
                i = (InputStream) m.getContent();
                bis = new BufferedInputStream(i,1024 * 8);
                out = new ByteArrayOutputStream();
                int len=0;
                byte[] buffer = new byte[1024];
                while((len = bis.read(buffer)) != -1){
                    out.write(buffer, 0, len);
                }
                out.close();
                bis.close();
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] data = out.toByteArray();
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            //Drawable d = Drawable.createFromStream(i, "src");
            return bitmap;
        }

    本文出自 Ray-Ray的博客

    文章地址 http://www.cnblogs.com/rayray/archive/2013/02/28/2936565.html

    感谢大家的推荐和收藏

  • 相关阅读:
    Oracle中字符串连接的实现方法
    Could not Open Install.Log File解决方法
    重新注册IIS
    Sqlserver 语法总结
    无法向会话状态服务器发出会话状态请求
    Windows Live Writer发布测试
    IIS不支持apk文件下载
    “未能加载文件或程序集“×××”或它的某一个依赖项。试图加载格式不正确的程序”问题的解决
    IIS安装
    Eclipse运行内存溢出
  • 原文地址:https://www.cnblogs.com/rayray/p/2936565.html
Copyright © 2011-2022 走看看