-3

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();
}
Rahul Mane
  • 1,005
  • 18
  • 33
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this. Most likely, you will find that you are crashing with a `NetworkOnMainThreadException`: http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception – CommonsWare Jun 03 '16 at 16:50
  • http://stackoverflow.com/a/3090802/5915572 – Dan Cantir Jun 03 '16 at 18:45

2 Answers2

0

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(); 
Kara
  • 6,115
  • 16
  • 50
  • 57
Gil Snovsky
  • 210
  • 2
  • 6
0

Did you search about Picasso library?

With picasso you can bind a bitmap from image request on your View(ie. ImageView). Doesn't matter how it works.

Picasso.with(context)
        .load(imageUri)
        .into(viewToAttach);

You can catch errors like Timeout or error response, with callbacks.

Franklin Hirata
  • 290
  • 2
  • 12