Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Copy Files (all at the same time)
    primarykey
    data
    text
    <p>I am currently working on a basic file browser for android. I have a working version for copying files, however as it works its way through directories it copies the files it finds. I want to change that so that I can find the total size of all files before starting to copy, as to help with a better progress bar.</p> <p>If there is another way to find the total size of a directory and all its contents?</p> <p>Here is my current version. I am having trouble changing this, I have tried using an arrayList however when I try to copy the files at the end, I think they are trying to copy in the wrong order.</p> <pre><code>public void copyDirectory(File sourceLocation , File targetLocation) throws IOException { if (sourceLocation.isDirectory()) { if (!targetLocation.exists() &amp;&amp; !targetLocation.mkdirs()) { throw new IOException("Cannot create directory: " + targetLocation.getAbsolutePath()); } String[] children = sourceLocation.list(); for (int i = 0; i &lt; children.length; i++) { copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i])); } } else { File directory = targetLocation.getParentFile(); if (directory != null &amp;&amp; !directory.exists() &amp;&amp; !directory.mkdirs()) { throw new IOException("Cannot create directory: " + directory.getAbsolutePath()); } FileInputStream in = new FileInputStream(sourceLocation); FileOutputStream out = new FileOutputStream(targetLocation); long fileLength = sourceLocation.length(); byte[] buf = new byte[1024]; long total = 0; int len; while ((len = in.read(buf)) &gt; 0) { out.write(buf, 0, len); total += len; publishProgress((int) (total * 100 / fileLength)); } in.close(); out.close(); } } </code></pre> <p><strong>Solution</strong></p> <p>jtwigg's answer should also work. I just thought I would add the solution I found. Can't answer my own question so I will put it here.</p> <p>Looping through all the files in the directory and keeping a running total seems to work. Although it requires looping first for the size and again to actually copy the files. Just call getDirectorySize() with the file or directory you wish to copy before calling copyDirectory().</p> <pre><code>private void getDirectorySize(File sourceLocation) throws IOException { if (sourceLocation.isDirectory()) { String[] children = sourceLocation.list(); for (int i = 0; i &lt; children.length; i++) { getDirectorySize(new File(sourceLocation, children[i])); } } else { totalFileSize += sourceLocation.length(); } } </code></pre> <p>The function will require the global long totalFileSize, and then all that is required is to replace:</p> <pre><code>publishProgress((int) (total * 100 / fileLength)); </code></pre> <p>with:</p> <pre><code>publishProgress((int) (total * 100 / totalFileSize)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload