4

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();
        }

    }
steo
  • 293
  • 1
  • 4
  • 15

2 Answers2

0

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.

lordmegamax
  • 2,684
  • 2
  • 26
  • 29
0

What you'll need to do is to create a progress bar object (you can add it into your layout if you want or create it programmatically) then update its state inside your download while loop. The means of creating a progress bar can be found here: http://developer.android.com/reference/android/widget/ProgressBar.html

The Guy
  • 129
  • 1
  • 6
  • Iv already got a progress bar working in the first code, that code downloads from the internet, the second code i use to transfer from sd to internal , internal to sd ect.. what i need is either the first code to be able tranfer files while showing the progress or the second code to show the progress. – steo Feb 12 '12 at 09:43