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;
}
}
}
Above is the code snippet which I use to load image from web into ImageButton. Most of the images get displayed , but certain urls like the one above i.e. url1 , Drawable.createFromStream returns null !! What is the reason and how to avoid it or overcome this problem ?