tags:

views:

28

answers:

1

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

    }

}

}

Hello all, i am using this code to display image from URL, my code is run and it does not gives error but image is not display.. any help?? Thanks..

+1  A: 

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
Thanks for reply
XXXXXX
Using this code i run application but image does not display..
XXXXXX
i see the log and it shows that "request time failed java.net.SocketException:Address family not supported by protocol"
XXXXXX
What should i do for fix this problem
XXXXXX
added new fix... tested and it should work
Aaron Saunders