【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

感谢大家的推荐和收藏

### 函数功能概述 `urlFilter_lru_cache_get` 函数的主要功能是从 LRU(Least Recently Used)缓存中查找指定 URL 的相关信息,如节点状态和分类位图,并将这些信息返回给调用者。 ### 函数参数分析 - `char *url`:要查找的 URL 字符串。 - `int *node_status`:用于存储找到的节点的状态。 - `bitmap *categories_bitmap`:用于存储找到的节点的分类位图。 - `int categories_bitmap_len`:分类位图的长度。 ### 函数代码示例及详细讲解 ```c static bool urlFilter_lru_cache_get(char *url, int *node_status, bitmap *categories_bitmap, int categories_bitmap_len) { if (NULL == url || NULL == node_status || NULL == categories_bitmap) { return false; } struct urlFilter_lru_node *urlFilter_node; struct hlist_node *pos, *n; // 遍历 LRU 缓存中的哈希表 hlist_for_each_entry_safe(urlFilter_node, pos, n, &lru_head.hash_table[hash_function(url)], hlist) { if (strcmp(urlFilter_node->domain, url) == 0) { // 找到匹配的 URL *node_status = urlFilter_node->status; memcpy(categories_bitmap, urlFilter_node->categories_bitmap, categories_bitmap_len); // 将该节点移动到 LRU 链表头部,表示最近使用 list_move(&urlFilter_node->list, &lru_head.lru_list->node_list); return true; } } // 未找到匹配的 URL return false; } ``` ### 代码详细解释 1. **参数检查**: ```c if (NULL == url || NULL == node_status || NULL == categories_bitmap) { return false; } ``` 检查传入的参数是否为空。如果任何一个参数为空,函数将返回 `false`,表示查找失败。 2. **遍历 LRU 缓存的哈希表**: ```c hlist_for_each_entry_safe(urlFilter_node, pos, n, &lru_head.hash_table[hash_function(url)], hlist) ``` 使用 `hlist_for_each_entry_safe` 宏遍历 LRU 缓存的哈希表中与 `url` 对应的哈希桶。`hash_function(url)` 是一个哈希函数,用于计算 `url` 对应的哈希值。 3. **查找匹配的 URL**: ```c if (strcmp(urlFilter_node->domain, url) == 0) { *node_status = urlFilter_node->status; memcpy(categories_bitmap, urlFilter_node->categories_bitmap, categories_bitmap_len); // 将该节点移动到 LRU 链表头部,表示最近使用 list_move(&urlFilter_node->list, &lru_head.lru_list->node_list); return true; } ``` 如果找到匹配的 URL,将该节点的状态和分类位图复制到传入的参数中,并将该节点移动到 LRU 链表的头部,表示该节点最近被使用过。最后返回 `true`,表示查找成功。 4. **未找到匹配的 URL**: ```c return false; ``` 如果遍历完整个哈希桶都没有找到匹配的 URL,函数将返回 `false`,表示查找失败。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值