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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:51:41+00:00 2026-05-25T00:51:41+00:00

I have been following a tutorial that remotely downloads an image to an imageview,

  • 0

I have been following a tutorial that remotely downloads an image to an imageview, but i’m not sure how to add a progress dialog (image or something) to show the user that image is downloading, instead of just a blank screen.

Hope someone can help

 ImageView imView;
 String imageUrl="http://domain.com/images/";
 Random r= new Random();
/** Called when the activity is first created. */ 
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
            WindowManager.LayoutParams.FLAG_FULLSCREEN );

    setContentView(R.layout.galleryshow);

    Button bt3= (Button)findViewById(R.id.get_imagebt);
    bt3.setOnClickListener(getImgListener);
    imView = (ImageView)findViewById(R.id.imview);
}    

View.OnClickListener getImgListener = new View.OnClickListener()
{

      @Override
      public void onClick(View view) {
           // TODO Auto-generated method stub


           int i =r.nextInt(114);
           downloadFile(imageUrl+"image-"+i+".jpg");
           Log.i("im url",imageUrl+"image-"+i+".jpg");
      }

};


Bitmap bmImg;
void downloadFile(String fileUrl){
      URL myFileUrl =null;          
      try {
           myFileUrl= new URL(fileUrl);
      } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
      try {
           HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
           conn.setDoInput(true);
           conn.connect();
           int length = conn.getContentLength();
           InputStream is = conn.getInputStream();

           bmImg = BitmapFactory.decodeStream(is);
           imView.setImageBitmap(bmImg);
      } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
 }
}
  • 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-25T00:51:42+00:00Added an answer on May 25, 2026 at 12:51 am

    You need to look at ProgressBar class and basically use that while the image is loading. Alternatively you could put default image while the image is being downloaded.

    To put the progress bar in your code, the easiest basically just flip their visibility in the layout.

    1. In your layout, you have two things. One placeholder for ProgressBar and the other is for the image
    2. The progressbar is set to VISIBLE initially and the image to GONE
    3. After you execute the AsyncTask (see below), you need to flip the visibility. Basically change the progressBar to GONE and the image to VISIBLE

    Here is what you should try to do. Check the NOTE and TODO comment in the code. Note: I just modified your code, but haven’t run it, but this should be enough to illustrate the idea.

    Some key points:

    1. Long running task that might block UI Thread should get executed in AsyncTask. In your case, this would be downloading the image
    2. Post execution that needs to be handled in UI Thread should be handle in postExecute()
    3. Doing e.printStacktrace() during catching Exception is not a good practice. Without appropriate handles, this exception is not being handled correctly and might cause bugs in the future. In addition, during production, this information doesn’t help you at all when bugs occur on the client’s side since it is merely printing out in the console
    
    
        View.OnClickListener getImgListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                // NOTE: here you need to show the progress bar, you could utilize ProgressBar class from Android
                // TODO: Show progress bar
    
                AsyncTask asyncTask = new AsyncTask() {
                    @Override
                    public Bitmap doInBackground(Void... params) {
                        int i =r.nextInt(114);
    
                        // NOTE: move image download to async task
                        return downloadFile(imageUrl+"image-"+i+".jpg");
                    }
    
                    @Override
                    public void onPostExecute(Bitmap result) {
                        // TODO: hide the progress bar here 
                        // and flip the image to VISIBLE as noted above
                        if(result != null) {
                            imView.setImageBitmap(result);
                        } else {
                            // NOTE 3: handle image null here, maybe by showing default image
                        }
                    }
                };
    
                // NOTE: execute in the background so you don't block the thread
                asyncTask.execute();
            }
        };
    
        // Change the return type to Bitmap so we could use it in AsyncTask
        Bitmap downloadFile(String fileUrl){
            URL myFileUrl =null;          
            try {
                myFileUrl= new URL(fileUrl);
            } catch (MalformedURLException e) {
                // NOTE: You should not have e.printStacktrace() here. In fact
                // printStacktrace is a bad practice as it doesn't really do anything
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                int length = conn.getContentLength();
                InputStream is = conn.getInputStream();
    
                Bitmap bmImg = BitmapFactory.decodeStream(is);
    
                // return image this to the main Thread
                return bmImg;
            } catch (IOException e) {
                return null;
            }
        }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been following some MVC tutorials that connect to a sql mdf database
I'm a newcomer to Rails, and have been following the tutorial on the rails
I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
Hi I am quite new to php but i have been following some tutorials
I am new to Ajax and have been following a tutorial from JQuery.com. I
i have been following this tutorial for the pagerAdapter. http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ My problem is that
I'm new to jQuery and have been following the tutorial here for a Panel
I am in the process of learning cocos2d-android. I have been following a tutorial
I have been following a tutorial on the internet it can be found at
I have been following this tutorial, online http://ruby.railstutorial.org/chapters/sign-in-sign-out?version=3.2#top and in part 8.2.3 there is

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.