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)