About URLs getting picture resources from the server

Source: Internet
Author: User

============ Problem Description ============


Why do you write this? Unable to get picture resources on the server
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Myimg= (ImageView) Findviewbyid (r.id.imagetest);
try {
URL url=new url (url1);
InputStream Is=url.openstream ();
Bitmap=bitmapfactory.decodestream (IS);
Myimg.setimagebitmap (bitmap);
Is.close ();

} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}
}
}
You can write it in a different way.
protected void OnCreate (Bundle savedinstancestate) {

New Thread ()
{
public void Run ()
{
try {
URL url = new URL (URL1);
InputStream Is=url.openstream ();
Bitmap=bitmapfactory.decodestream (IS);
Is.close ();
} catch (Exception e) {
TODO Auto-generated catch block
E.printstacktrace ();
}
Myhandler.sendemptymessage (0);
};
}.start ();
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Myimg= (ImageView) Findviewbyid (R.ID.IMAGETEST1);

Myhandler=new Handler ()
{
public void Handlemessage (Message msg)
{
if (msg.what==0)
{
Myimg.setimagebitmap (bitmap);
}
};
};

}
}
Ask the great God for advice, the first reason why the picture is not displayed

============ Solution 1============


Because you performed network operations in the main thread of the UI.

About URLs getting picture resources from the server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.