0

I know that there are already a bunch of questions about this on StackExchange but I've already tried a couple of suggestions and I can't seem to find the right solution for me.

What I want to do: I copied the file test.txt into the folder "test" (normal External Storage, no additional SD card) on my phone using my PC and now I want to read the content of the file again, copy everything into a String and do more stuff with it.

On my PC the path of the file is: Computer\Samsung Galaxy S7\Phone\test\test.txt On the phone it's: My Files\Device Storage\test\test.txt

My code:

String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String path = root + File.separator + "test" + File.separator + "test.txt";

File f = new File(path);
StringBuilder sb = new StringBuilder();
BufferedReader br = null;

try {
    br = new BufferedReader(new FileReader(f));
    String line;

    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append('\n');
    }

    text = sb.toString();
    br.close();
} catch (IOException e) {
    return path;
}

The problem is that it always gets into the "catch" part and then returns "Storage/emulated/0/test/test.txt", which apparently isn't the right path (otherwise it should have found the file).

If I use

File f = Environment.getExternalStoragePublicDirectory("test" + File.separator + "test.txt");

it results in the same thing.

What did I miss/what do I have to use, so it finds the file?

Neph
  • 1,823
  • 2
  • 31
  • 69
  • "What did I miss" -- well, you missed logging the exception. **Never** catch an exception without logging it somewhere. For example, you may have a permissions problem. You are also using string concatenation instead of the proper `File` constructor (use `File f=new File(Environment.getExternalStorageDirectory(), "test" + File.separator + "test.txt")`. – CommonsWare Apr 18 '16 at 17:18
  • "If I use... it results in the same thing" -- that is because your value passed into `getExternalStoragePublicDirectory()` is not one of [the documented values](http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29). – CommonsWare Apr 18 '16 at 17:19
  • "so what else leaves that in terms of permissions?" -- see http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it "where it actually shows the output for testing with a connected phone" -- the same as for an emulator, see http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Apr 18 '16 at 20:26
  • I did add the "read" permission for both ext./int. storage to the Manifast and it's a simple, vanilla txt-file, so what else leaves that in terms of missing permissions? Also, if you tell me, where it actually shows the output for exceptions (and for System.out.println would be nice too) with a connected phone, I'd be happy to look at it. It doesn't show it in the normal console. Does it actually change anything what constructor of "File" I use? And yup, I definately forgot about the enums, thanks for reminding me. I want to use my own folder, so I guess I can't use that then. – Neph Apr 18 '16 at 20:31
  • "Does it actually change anything what constructor of "File" I use?" -- your approach assumes that `Environment.getExternalStorageDirectory()` never returns a value with a trailing separator (e.g., `foo/`). I would not make that assumption. The `File` constructor should handle that. – CommonsWare Apr 18 '16 at 20:33
  • It actually was the permission problem you mentioned ("IOException: java.io.FileNotFoundException: /storage/emulated/0/test/test.txt: open failed: EACCES (Permission denied)"). It's kind of fixed now - the GearVR framework sadly doesn't work properly with extra permissions yet :/ but at least it's running and it can read the file now. Thank you! – Neph Apr 21 '16 at 17:39

0 Answers0