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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:14:18+00:00 2026-06-13T09:14:18+00:00

i use the following code for downloading a file from the internet. using this

  • 0

i use the following code for downloading a file from the internet. using this code i have a progress dialog

private void startDownload() {
        String url = tv.getText().toString();
        new DownloadFileAsync().execute(url);
    }

    class DownloadFileAsync extends AsyncTask<String, String, String>  {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected String doInBackground(String... aurl) {
            int count;

            try {

                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(
                        "data/data/com.xxxxxxxx.android/app_p_pic/"
                                + "blabla" 
                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {

                e.printStackTrace(); 

            }
            return null;

        }

        protected void onProgressUpdate(String... progress) {
            Log.d("ANDRO_ASYNC", progress[0]);
            mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }


    }
}

is there a way of using the above code to transfer a file from say internal to sd? at the momment i use the following code but iv been unable to get the progress to work with progress bar

protected void ontrans() {
        String title = tv.getText().toString();

        try {

            myInput = new FileInputStream(title);


            File directory = new File(Environment.getExternalStorageDirectory()
                    + "/android_files/data/secure");
            if (!directory.exists()) {
                directory.mkdirs();
            }

            OutputStream myOutput = new FileOutputStream(
                    Environment.getExternalStorageDirectory()
                            + "/android_files/data/secure/" + chosenFile);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();

            myOutput.close();

            myInput.close();
            Toast.makeText(File_exActivity.this, "file move Succesfully!",
                    Toast.LENGTH_SHORT).show();
            showDialog(11);
        } catch (FileNotFoundException e) {
            Toast.makeText(File_exActivity.this, "no file found!",
                    Toast.LENGTH_LONG).show();

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            Toast.makeText(File_exActivity.this, "move unsuccesfull!",
                    Toast.LENGTH_LONG).show();

            // 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-06-13T09:14:19+00:00Added an answer on June 13, 2026 at 9:14 am

    Things to think about: you can get the size of the source file File.length() and destination. Comparing them to display the difference in the progress bar.

    The File.length() method returns the following according to the javadoc:

    The length, in bytes, of the file denoted by this abstract pathname,
    or 0L if the file does not exist. Some operating systems may return 0L
    for pathnames denoting system-dependent entities such as devices or
    pipes.

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

Sidebar

Related Questions

I have some code similar to the following for downloading a binary file. My
I'm trying to use the following code to create a zip file from a
I have an array of objects, and i use following code to get in
I have use following code to display category and there product on click patent
I need to execute a batch file (in Java 6) and use following code
I use following code to delete old image from ftp... unlink(getcwd() . '/images/' .
I have the following task: download a file using HTTPS and authentication. Indy seems
I am downloading an image from the internet which has the following dimensions: 128px
I use following code to select file but i can select one file only.
I am using wxWidgets 2.9.2, when i use following code //ToolBar wxToolBar *mainTool =

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.