1

I am having no end of trouble accessing a file that I know exists for using it in a BufferedReader. Here's the code:

File file = new File(Environment.DIRECTORY_DOWNLOADS + getString(R.string.dloaded_latest_numbers_dir) + getString(R.string.dloaded_latest_numbers_file));
BufferedReader buffedReader = new BufferedReader(new FileReader(file));

But every time I hit the BufferedReader line I get "java.io.FileNotFoundException: /Download/MyDir/MyFile.txt: open failed: ENOENT (No such file or directory)"

I am displaying the file location in a TextView with:

TextView displayFileLocation = (TextView) findViewById(R.id.readLocTextView);
displayFileLocation.setText("location is: " + file.getAbsolutePath());

Which displays the correct full path to the file. (/Download/MyDir/MyFile.txt)

My AndroidManifest.xml file has the following permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

logcat output is:

07-08 13:06:28.556  15351-15351/au.com.acent.ash.basiclottochecker D/OpenGLRenderer﹕ Enabling debug mode 0
07-08 13:06:43.451  15351-15351/au.com.acent.ash.basiclottochecker W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ java.io.FileNotFoundException: /Download/MyDir/MyFile.txt: open failed: ENOENT (No such file or directory)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:420)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.io.FileReader.<init>(FileReader.java:42)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:64)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.Posix.open(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:404)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ ... 17 more

I am absolutely positive this file exists and I have used DownloadManager to download this file to the exact same place using the exact same location reference for the directory location:

File file = new File(Environment.DIRECTORY_DOWNLOADS + getString(R.string.dloaded_latest_numbers_dir));

What am I doing wrong?? Please help!

Psyonic
  • 195
  • 9
  • @JigarJoshi doesn't seem to be the same issue (at first view at least). – matiash Jul 08 '14 at 03:25
  • @matiash why do you think its not duplicate ? – jmj Jul 08 '14 at 03:25
  • @JigarJoshi Well, he claims he was able to save a file there. So I assume it should be a valid path. – matiash Jul 08 '14 at 03:28
  • did you check the question I linked here ? – jmj Jul 08 '14 at 03:29
  • This is true @matiash. I just update the question to address why it's not a duplicate – Psyonic Jul 08 '14 at 03:29
  • @JigarJoshi Well the OP seemed sure. Now I think you're right. Sorry. :) – matiash Jul 08 '14 at 03:31
  • @Ash Are you _positively_ sure you've saved the file there? Because it shouldn't have been possible. – matiash Jul 08 '14 at 03:31
  • I've been stuck on this for about 1.5 days now, I've read so many different ways of doing this, like with FileInputStream, InputStream, and alike which I know wont do what I want but I just wanted to see if I got the same File Not Found error, which I did – Psyonic Jul 08 '14 at 03:33
  • Yep, I downloaded the file from a location on the net and watched it come down using File Commander. It's defiantly there. I just changed it's name here for clarity – Psyonic Jul 08 '14 at 03:34
  • Just curious, why should it not be posible? – Psyonic Jul 08 '14 at 03:36
  • @matiash Update: It seams that when I download the file it goes to a path relative to the Internal Storage path, which I believe is why you thought it impossible to write to the path I was showing. Being a Linux guy I figured if the shown path starts at / then it must be absolute root. I was wrong, the FULL path is /storage/emulated/0/Download/MyDir/MyFile. – Psyonic Jul 08 '14 at 04:47

1 Answers1

1

Your code is trying to access /Download/MyDir/MyFile.txt, which doesn't seem like the correct place, since that file doesn't exist in the root folder. It is probably inside the sdcard directory. To get the correct directory use

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

instead of

Environment.DIRECTORY_DOWNLOADS
Hiemanshu Sharma
  • 7,702
  • 1
  • 16
  • 13
  • So this now downloads my file to /storage/emulated/0/Download/MyFile.txt and when I try to access it into a BufferedReader same result: File Not Found – Psyonic Jul 08 '14 at 03:56
  • How are you downloading it? And where is file downloaded to? – Hiemanshu Sharma Jul 08 '14 at 03:58
  • I don't see why that's any different to the original location. Both start at / and both I can write to. The original location is the "Download" directory when you use a file browser like File Commander. Am I missing something? – Psyonic Jul 08 '14 at 03:59
  • I am downloading using DownloadManager which all works fine. I'm using the exact same line `File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));` for the file location as in the BufferedReader (and I had to encase your code in a String.valueOf() to get it to work. Do you think that matters? – Psyonic Jul 08 '14 at 04:03
  • You can just use .toString() at the end instead of String.valueOf, and as far as the first location goes it was trying to access /Download/MyFile.txt instead of the place where it was downloaded to. You also need to append the file path so it would be something like `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/path/to/file` – Hiemanshu Sharma Jul 08 '14 at 04:42
  • What you originally answered was correct! I was, mistakenly, assuming that I should also be using it for the the path to download my file to using DownloadManager but DownloadManager must take care of that stuff internally because using it there downloaded the file to /storage/emulated/0/Download/storage/emulated/0/MyDir/MyFile.txt. Thanks! – Psyonic Jul 08 '14 at 05:06