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 7659651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:21:38+00:00 2026-05-31T13:21:38+00:00

when I run a following code Runnable r = new Runnable() { @Override public

  • 0

when I run a following code

    Runnable r = new Runnable() {

    @Override
    public void run() {

         final String urlString = "http://10.0.2.2/conn/s.png";
            // URL newurl = null;

                     try{   
                do
                {
                            if (ch==1)
                            {
                                //Thread.sleep(7000);
                                HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
                                HttpClient httpClient = new DefaultHttpClient();
                                HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
                                HttpEntity entity = response.getEntity();
                                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
                                    InputStream is = bufHttpEntity.getContent();
                                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                                   im.destroyDrawingCache();
                                    im.setImageBitmap(bitmap);   
                                    bitmap=null;
                                   // Thread.sleep(3000);
                                    out.write(1);
                                   out.flush();




                                 }  
                }
                while(( ch=in.read()) == 1);
                     }
                     catch(IOException ex)
                     {

                     }
                    catch(URISyntaxException ex)
                    {}
    }
};

and the thread is initialized like this:

                  t = new Thread(r);
          t.start();

I am getting the following exception:

03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.View.requestLayout(View.java:7918)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.View.requestLayout(View.java:7918)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.View.requestLayout(View.java:7918)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.View.requestLayout(View.java:7918)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.view.View.requestLayout(View.java:7918)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.widget.ImageView.setImageDrawable(ImageView.java:306)
03-18 01:20:50.399: E/AndroidRuntime(305):  at android.widget.ImageView.setImageBitmap(ImageView.java:320)
03-18 01:20:50.399: E/AndroidRuntime(305):  at remote.presentation.rshare$1.run(rshare.java:124)
03-18 01:20:50.399: E/AndroidRuntime(305):  at java.lang.Thread.run(Thread.java:1096)
  • 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-05-31T13:21:39+00:00Added an answer on May 31, 2026 at 1:21 pm

    You are changing the UI from a thread, can’t do that in Android. It has to be done on the UI thread. I prefer AsyncTasks to using Threads. This AsyncTask will get an image from a url.

    The class

    private class GetImage extends AsyncTask<String,Void,Bitmap>{
    
        private ImageView view;
    
        public GetImage(ImageView iv){
             this.view = iv;
        }
    
        protected Bitmap doInBackground(String... params) {
                        //background thread
            try {
    
                return BitmapFactory.decodeStream((InputStream)new URL(params[0]).getContent());
            } catch (MalformedURLException e) {
                e.printStackTrace();
    
            } catch (IOException e) {
                e.printStackTrace();
    
            }
    
            return null;
    
        }
        @Override
        protected void onPostExecute(Bitmap b){
            if(b != null)
                this.view.setImageBitmap(b); 
        }
    }
    

    to use it

    new GetImage(myImageView).execute(someURL);
    

    EDIT (reason for edit, see comments) – the following code will repeatedly download the same image as the question asker wanted

    Create a class variable for the GetImage class

    GetImage getImageInstance = new GetImage(myImageView); 
    getImageInstance.execute(url);
    

    Modified class

    private class GetImage extends AsyncTask<String,Bitmap,Void>{
    
        private ImageView view;
        Bitmap bitmap = null;
        long lastUpdateTimestamp = System.currentTimeMillis();
        public GetImage(ImageView iv){
             this.view = iv;
        }
    
        protected Bitmap doInBackground(String... params) {
                        //background thread
            while(running){
                try {
                    //if more than 2 seconds have elapsed update the image
                    if((System.currentTimeMillis() - lastUpdateTimestamp) > 2000){
                        bitmap = BitmapFactory.decodeStream((InputStream)new URL(params[0]).getContent());
                        pubilshProgress(bitmap);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
    
                } catch (IOException e) {
                     e.printStackTrace();
    
                }
            }
            return null;
    
        }
        //this method that will update the UI
        @Override
        protected void onProgressUpdate(Bitmap... images){
            if(images[0] != null)
                this.view.setImageBitmap(images[0]);
            lastUpdateTimestamp = System.currentTimeMillis();
        }
        @Override
        protected void onPostExecute(Void unused){
            bitmap.recycle();
        }
        public void stopDownloading(){
            running = false;
        }
    }
    

    To stop downloading

    getImageInstance.stopDownloading();
    

    I don’t know what your app does but users will not like downloading unnecessary images which eats into their data allowance, perhaps think about a better way to do what you want to do.

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

Sidebar

Related Questions

my code is the following onLoadCallback = new Runnable() { @Override public void run()
I try to run the following code: public void Init(Url rootUrl) { var web
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap
Consider the following code snipet: public class A { private final Executor executor =
This is the snippet : protected void paintComponent(final Graphics g) { Runnable r=new Runnable()
when I trying to run following code. var result = from c in db.brand
When I run the following code to test copying a directory, I get a
I want to run the following code: ajaxUpdate(10); With a delay of 1 second
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
When I run the following code, it only seems to be downloading the first

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.