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
感謝大家的推薦和收藏