文件传输到SD的进度条

时间:2012-02-11 19:21:38

标签: android file progress transfer

我使用以下代码从互联网上下载文件。使用此代码我有一个进度对话框

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


    }
}

有没有办法使用上面的代码将文件从内部传输到sd?在妈妈,我使用以下代码,但iv无法使用进度条获得进度

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

    }

2 个答案:

答案 0 :(得分:0)

您需要做的是创建一个进度条对象(如果您想要或以编程方式创建它,可以将其添加到您的布局中),然后在下载循环中更新其状态。可以在此处找到创建进度条的方法:http://developer.android.com/reference/android/widget/ProgressBar.html

答案 1 :(得分:0)

需要考虑的事项:您可以获得源文件File.length()和目标的大小。比较它们以显示进度条中的差异。

File。length()方法根据javadoc返回以下内容:

  

此抽象路径名表示的文件的长度(以字节为单位),   如果文件不存在,则返回0L。某些操作系统可能会返回0L   用于表示系统相关实体(如设备或)的路径名   管道