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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:23:28+00:00 2026-06-03T15:23:28+00:00

In my android application I am trying create persistent connection for downloading images. Here

  • 0

In my android application I am trying create persistent connection for downloading images. Here is my code

 public class PersActivity extends  Activity {
    ImageView img;
    String imageUrl="http://192.168.1.4/JRec/";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button bt3= (Button)findViewById(R.id.download);
        bt3.setOnClickListener(getImgListener);
        img = (ImageView)findViewById(R.id.image);
    } 

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

        @Override
        public void onClick(View view) {


            for (int i = 0; i<4;i++){
            downloadFile(i,imageUrl+"images/"+i+".png");

            }


        }

    };

    Bitmap bmImg;
    void downloadFile(int i, String fileUrl){

        URL myFileUrl =null; 
        try {
            myFileUrl= new URL(fileUrl);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            System.out.println("Opening connection");
            HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setDoInput(true);
            conn.connect();


            System.out.println("Downloading"+fileUrl);
            InputStream is = conn.getInputStream();

            bmImg = BitmapFactory.decodeStream(is);
            img.setImageBitmap(bmImg);

            if (i ==3){
                System.out.println("Disconnected");
                conn.disconnect();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

When I start download the file every time it should open connection for download, I want to open connection only one time and when the application end downloading all files the connection must be disconnected. Is there any way to do this.

Thanks anyone who get attention on this.

  • 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-03T15:23:30+00:00Added an answer on June 3, 2026 at 3:23 pm

    Http connections are “stateless” so there is no connection to be kept open like say in an SSH connection;
    So each image you download will be done in a separate request/connection. (that’s also the web browser do it).

    Also, why did you put the creation of the URL in a separate try/catch ?
    That makes no sense because if you fail to create the URL object, you will get a null pointer when you will try to open the connection.

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

Sidebar

Related Questions

I'm trying to create a child class of Android.App.Application to override OnCreate(), but I
I'm trying to create a setup activity for my Android application which my users
I have this code for an Android application I'm trying to create into interact
I am trying to create a simple Android application that has a ActivityList of
I am currently trying to create an Android application that loops Audio from the
I am trying to save a picture in an Android application. Here is my
I'm trying create a scaling layout for an Android application. The whole screen should
I am trying to create an android application that is linked to a QR
I've been trying to create an android-application the last couple of weeks, and mostly
I'm trying to create define a custom Application class as follows: create an empty

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.