-1

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

    }
Community
  • 1
  • 1
user1324674
  • 384
  • 1
  • 3
  • 12

1 Answers1

0

You can't read anything in /data/data except from subdirectories controlled by your app.

/data/data/*packagename*/files

is the path returned by Context.getFilesDir()

/data

is the path returned by Environment.getDataDirectory().

Where exactly do you expect the files to be, and how did they get there originally?

Joe Malin
  • 8,621
  • 1
  • 23
  • 18