i'm having trouble figuring out why my app is crashing every time i open it. It's only when I uncomment the for loop.
I'm using this idea here to copy files from one directory to another
How to programmatically move, copy and delete files and directories on SD?
however this part (i tried both this way and the same way as in the link above, using children string, etc)
for (File file : files)
{
CopyFile(file, target);
}
keeps causing my application to crash. I tried commenting out just this part and the app runs fine (it doesnt copy anything however as its not accessing a file)
Any ideas?
private void CopyFile(File source, File target) throws IOException {
if (source.isDirectory()){
File[] files = source.listFiles();
for (File file : files)
{
CopyFile(file, target);
}
} else {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
}