-1

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.

Jakub H.
  • 56
  • 5
  • 1
    Have you used file explorer to check if the file was downloaded or not? – TSR Oct 15 '16 at 13:19
  • I have now, but there is no sign of the file. – Jakub H. Oct 15 '16 at 13:41
  • Probably downloading is ok but saving to file not. You should immediately check if the file exists. Not later. Probabky you have unnoticed exceptions and no code in catch blocks. – greenapps Oct 15 '16 at 13:47
  • `At which point, on real devices, I get an error: ENOENT`. No not at that point. You only create a File object. – greenapps Oct 15 '16 at 13:49
  • You also did not tell for which android version you compile or which device version. – greenapps Oct 15 '16 at 13:51
  • `Unable to open downloaded files on real devices`. Strange description of your problem when there is no file to begin with. – greenapps Oct 15 '16 at 14:00
  • You are right, I have edited my question accordingly. – Jakub H. Oct 15 '16 at 14:20
  • @JakubH. what version of Android do your devices run? –  Oct 15 '16 at 14:37
  • Thanks for the heads up, there is definitely something going on with different Android versions, see my edit. – Jakub H. Oct 15 '16 at 14:52
  • Yes. And we know what is the problem for android 6.0. You found a solution. But before you have ignored exceptions and stacktraces and messages that the file could not be written. You ignored them and had no code in catch blocks to inform the user. You will be bitten again once if you do not account for them. – greenapps Oct 15 '16 at 16:54

2 Answers2

2

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);
            }
        }
Community
  • 1
  • 1
Jakub H.
  • 56
  • 5
1

try to replace:

`Environment.getExternalStorageDirectory().toString()`

with

`Environment.getExternalStorageDirectory().getAbsolutePath()`
Atiq
  • 14,435
  • 6
  • 54
  • 69