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が特定のURLでnullを返す
を使用している、それは2.xの中でまだ残っているのですか?私のテストではそうだが、私はGoogleから正式な確認を求めている。また、あなたのコードでは、BUFFER_IO_SIZEに何を設定しますか? – ThomasW
@ThomasW確かにわかりません。私が確信していることは、エミュレータとデバイスの両方で2.1と2.2で同じ動作と同じ修正が私のために働いたということです。私はBUFFER_IO_SIZEの値を含めるように答えを更新します。 – mcveat
ソリューションをありがとう、それは動作します。 – sat