2

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.jpg";

    @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) {

        }

    }
}
Kevin
  • 53,822
  • 15
  • 101
  • 132
XXXXXX
  • 21
  • 1
  • 2

1 Answers1

3

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

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80