public class TestButton extends Activity { /** Called when the activity is first created. */ ImageButton imgBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgBtn = (ImageButton) findViewById(R.id.image); //String url = "http://thenextweb.com/apps/files/2010/03/google_logo.jpg"; String url1 = "http://trueslant.com/michaelshermer/files/2010/03/evil-google.jpg"; Drawable drawable = LoadImage(url1); imgBtn.setImageDrawable(drawable); } private Drawable LoadImage(String url) { try { InputStream is = (InputStream) new URL(url).getContent(); Drawable d = Drawable.createFromStream(is, "src"); return d; } catch (Exception e) { return null; } } }
以上是我用來從web加載圖像到ImageButton中的代碼片段。大部分圖像都會顯示出來,但某些url像上面那樣,即url1,Drawable.createFromStream返回null!什麼原因,如何避免或克服這個問題?Android中的CreateFromStream返回null爲特定的網址
該缺陷是在安卓1.0版本,它仍然是圍繞2.X?在我的測試中,似乎是這種情況,但我正在尋求Google的官方確認。另外,在你的代碼中,你將BUFFER_IO_SIZE設置爲什麼? – ThomasW 2011-02-04 07:02:38
@ThomasW無法確定。我敢肯定的兩件事情是它在模擬器和設備上具有相同的行爲和相同的修復功能,適用於2.1和2.2版本。我會更新我的答案以包含BUFFER_IO_SIZE值。 – mcveat 2011-02-04 08:18:09
感謝您的解決方案,它的工作原理。 – sat 2011-02-10 09:39:42