Vako Xecuriani Vako Xecuriani - 4 months ago 11
Android Question

Image download from URL

I'm learning Android Programming and what's mistake?

ImageView im = (ImageView) findViewById(R.id.imageView);
try {
URL u = new URL("http");
HttpURLConnection ur = (HttpURLConnection) u.openConnection();
InputStream in = ur.getInputStream();
Bitmap bs = BitmapFactory.decodeStream(in);

im.setImageBitmap(bs);

in.close();
} catch (Exception e) {
e.printStackTrace();
}

Answer

You can't run on main Thread, just use new Thread:

Thread thread = new Thread(new Runnable()
{
    @Override
    public void run() 
    {
        try 
        {
            //Your code goes here
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
});

thread.start(); 
Comments