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; }
本文出自
文章地址
感谢大家的推荐和收藏