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

    感谢大家的推荐和收藏

  • 相关阅读:
    ISO9126 软件质量模型
    java 15.String
    java 14. ArrayList常用方法
    java 13. 方法重载构造方法块this用法
    linux ssh连接心跳检查断开连接
    关于递归,我有几句话想说
    pytest 报错 ImportError: cannot import name 'main'
    递归回溯剪枝之斐波那契数列
    appium-doctor诊断信息不完整
    数据驱动,关键字驱动,混合驱动简单模型
  • 原文地址:https://www.cnblogs.com/rayray/p/2936565.html
Copyright © 2011-2022 走看看