Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8255239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:27:27+00:00 2026-06-08T01:27:27+00:00

I need to get an image from the web and display it on the

  • 0

I need to get an image from the web and display it on the main activity of my app. I am trying to put it inside a separate thread, with the following code (it goes inside the onCreate method):

       Thread trd = new Thread(new Runnable(){
            @Override
            public void run(){
                ImageView iv1 = (ImageView) findViewById(R.id.promo);
                try{
                    URL url = new URL("http://www.domain.com/image.jpg");
                    InputStream content = (InputStream)url.getContent();
                    Drawable d = Drawable.createFromStream(content, "src");
                    iv1.setImageDrawable(d);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
        trd.run(); 

This code is working properly and updating the UI with the image I am getting from the web.

Questions:

  1. I heard that Android only allows you to modify the UI from the main UI thread, but it looks like with my code I am doing it from inside a separate thread. How come? Should I be worried my code might break for some reason?

  2. Is the above code efficient to make sure my app UI will not freeze while waiting for the web image to be retrieved?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:27:29+00:00Added an answer on June 8, 2026 at 1:27 am
    I heard that Android only allows you to modify the UI from the main UI thread, but it looks like with my code I am doing it from inside a separate thread. How come? Should I be worried my code might break for some reason?
    

    Yes you are modifying from a separate thread.

    to set drawable inside a thread use, and make d as final.

    SampleActivity.this.runOnUiThread(new Runnable() {
    
                        public void run() {
                            iv1.setImageDrawable(d);
    
                        }
                    });
    

    Is the above code efficient to make sure my app UI will not freeze while waiting for the web image to be retrieved?

    No as It will not run at all. and will through the Exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get image from the web and store it in the phone
I need to download an image from the web and display it in an
I need to get an image from a SQL Server as a byte[], and
I need fast to get the image from a game. And i try with
I am getting image and some details from .net web server. I need to
I need to get a file into memory in my app from a secured
I have some images I need to get from the web. Just using data
I am trying to display an image from a URL, which may be larger
I need to get array of images that have to call from server and
I need to store locally on android devices some images I get from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.