android - 文件传输到 sd 的进度条

标签 android file progress transfer

我使用以下代码从 Internet 下载文件。使用这段代码我有一个进度对话框

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


    }
}

有没有一种方法可以使用上面的代码将文件从 say internal 传输到 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();
        }

    }

最佳答案

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

File .length() 方法根据 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.

关于android - 文件传输到 sd 的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9243295/

相关文章:

android - 在 Android Market 中发布应用程序的优势

java - 使用两个斜线分隔转换的处理文件保存

java - 文件已删除,但实际文件并未删除。 (听起来不对)

c - 使用C通过cmd将参数传递给正在运行的文件

javascript - 如何根据离散步骤插值进度?

android - android 2.3.3 及更高版本中微调器的下拉模式

android - SQLite - 如何查询空间引用?

java - Android APP主线程内更新进度条

android - 如何优化在我的 Android 应用程序中加载大量字符串?

html - CSS 进度条样式问题