I am using this code to display an image from a URL. My code is run and it does not give an error but the image is not displayed. Any help?
Here is the code:
public class MainActivity extends Activity {
ImageView i;
String imageUrl = "http://64.250.238.26:1111/clips/sunsetsofmauisplash.jp开发者_StackOverflowg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
i = (ImageView) findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}
try this approach and the logging code will show you if there are any exceptions being thrown
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URL url = new URL(imageUrl);
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
ImageView i = (ImageView) findViewById(R.id.image);
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
Log.e("log", "bad url", t);
} catch (IOException e) {
Log.e("log", "io error", t);
}
}
Update:
After digging I found this Fix to the decoder error that was being logged
精彩评论