文件传输到 SD 的进度条

发布于 2025-01-04 11:26:17 字数 3536 浏览 5 评论 0原文

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

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?目前我使用以下代码,但我无法获取与进度条一起使用的进度

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

    }

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2025-01-11 11:26:17

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

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

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

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.

爱殇璃 2025-01-11 11:26:17

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文