-1

I have the following code that creates a FileInputStream to a csv file in the Download folder on my phone.

I can Log the length of the file but it is throwing a FileNotFoundException.

How can this be when i have the correct length outputted?

thanks

String fileName = "Download/file1.csv";
        String path = Environment.getExternalStorageDirectory()+"/"+fileName;
        File file = new File(path);

        Log.e(TAG, "file length = " + file.length());


        FileInputStream fin = null;
        try {
            // create FileInputStream object
            fin = new FileInputStream(file);

            byte fileContent[] = new byte[(int)file.length()];

            // Reads up to certain bytes of data from this input stream into an array of bytes.
            fin.read(fileContent);
            //create string from byte array
            String s = new String(fileContent);
            Log.e(TAG, "fileData = " + s);
        }
        catch (FileNotFoundException e) {

            Log.e(TAG, "File not found" + e);
        }
        catch (IOException ioe) {

            Log.e(TAG, "Exception while reading file " + ioe);
        }
        finally {
            // close the streams using close method
            try {
                if (fin != null) {
                    fin.close();
                }
            }
            catch (IOException ioe) {
                System.out.println("Error while closing stream: " + ioe);
                Log.e(TAG, "Error while closing stream: " + ioe);
            }
        }

.

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



03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/WriteTagActivity: file length = 523
03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/WriteTagActivity: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Download/file1.csv: open failed: EACCES (Permission denied)
turtleboy
  • 8,210
  • 27
  • 100
  • 199
  • 1
    @Selvin I'm aware of that but i am under the impression that if i target <23 i do not need to write the code that asks for permissions at runtime. – turtleboy Mar 02 '17 at 11:29
  • @Selvin look at AndyGeeky http://stackoverflow.com/questions/33030933/android-6-0-open-failed-eacces-permission-denied – turtleboy Mar 02 '17 at 11:32
  • Did you check if your app has permissions or not? – Selvin Mar 02 '17 at 11:33
  • @Selvin I am sorry, it is a new project i have started, the others ones i have been working on for years were set to 22, this one is set to 25...i forgot check. Again my mistake.. sorry – turtleboy Mar 02 '17 at 11:35
  • Just delete it ... anyway It would be better to add asking for permission anyway :) – Selvin Mar 02 '17 at 11:36
  • @Selvin agreed, i just have a few tight deadlines at the moment, but yes in the next version all my software will ask permissions at runtime. Thanks for your help – turtleboy Mar 02 '17 at 11:44

1 Answers1

0
//Create folder first
String dirPath = Environment.getExternalStorageDirectory()+"/Download";
        File dir = new File(dirPath);
        dir.mkdir();

//Create File
        File file = new File(dir,"file1.csv");
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Mar 02 '17 at 13:29