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

        }

    }
}

1 Answer 1

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

2
  • i see the log and it shows that "request time failed java.net.SocketException:Address family not supported by protocol"
    – XXXXXX
    Oct 23, 2010 at 4:51
  • What should i do for fix this problem
    – XXXXXX
    Oct 23, 2010 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.