I have just encountered a problem I can't seem to fix by myself.
A key element of this application is downloading and storing a file from a url, for which I am using an extension of AsyncTask
This file is eventually saved to
Environment.getExternalStorageDirectory().toString() + "/" + fileName + ".xls")
then a file is created:
File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + fileName + ".xls");
and a stream is made
InputStream is = new FileInputStream(file);
At which point I get a FileNotFoundException ENOENT : no such file or directory.
I have the appropriate permissions set up in my AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Environment.getExternalStorageState() returns "mounted" value.
And I can confirm internet connection is established, as before I can download any files, I have to log in with appropriate credentials in the app.
I am making this app for API 16, Android 4.1 (Jelly Bean). Any help would be greatly appreciated!
EDIT: I have managed to make make this work on Samsung SM-G800G (Android 5.1.1, API 22). But the application will not run on my Nexus One (Android 6.0, API 23) nor SAMSUNG SM-G900F (Android 6.0.1, API 23).
So there is definitely something going on with how Android versions affect the download, I am looking into it right now.
Thanks for the contribution everyone, the issue was elsewhere than I thought and could now easily be googled.
Thanks goes to this post by MetaSnarf
The issue is indeed with the way how Android 6.0+ (API 23+) handles permissions.
Putting <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in the AndroidManifest.xml file is NOT ENOUGH for the application to get the permission. I have fixed it by adding the following block in my code.
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
}
else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}