2

I am trying to read an Excel file in the documents folder (/storage/emulated/0/Documents/). I have gotten the Apache POI library included in my project, and it builds and runs. Upon opening the relevant activity, I run ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1001);. the public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) function is called properly, and it contains super.onRequestPermissionsResult(requestCode, permissions, grantResults); After calling that first function, I try to run the following line of code: InputStream inp = new FileInputStream(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOCUMENTS + "/" + "todo.xlsx"); For some reason, this error: open failed: EACCES (Permission denied) is displayed before it tells me that the permission is granted, which is a Log.d statement after super.onRequestPermissionsResult, but before my attempt to access the file with POI. I have the appropriate <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> line in AndroidManifest.xml.

Robert
  • 47
  • 6
  • You do not have access to files in `Documents/`, except for those that your app put there originally. – CommonsWare Jun 26 '22 at 22:23
  • So I assume that means that there exists no way to grant permissions to your app to allow it to read a file in there that it did not write? Where on the filesystem could I put it so that the app can read it? – Robert Jun 26 '22 at 22:37
  • 1
    "Where on the filesystem could I put it so that the app can read it?" -- locations identified by methods on `Context`, such as `getExternalFilesDir()`. Or, if you can work purely with streams, use the Storage Access Framework and let the user choose the file via `ACTION_OPEN_DOCUMENT` / `ActivityResultContracts.OpenDocument`. – CommonsWare Jun 26 '22 at 23:04
  • getExternalFilesDir() gave me `/storage/emulated/0/Android/data/com.example.planner/files` as the root directory of what is available to my app, but when I go into the emulator file browser, it says that I cannot view the contents of the data directory in `/storage/emulated/0/Android/` because permission is denied. Is there any other way for me to upload a file to the emulator from my computer then read it in the app? – Robert Jun 26 '22 at 23:45
  • The file browser has no access to your apps private storage location as it is another app. But your app has. – blackapps Jun 27 '22 at 02:15
  • Sorry to (kind of) ask the same thing twice, but is there any way to upload a file from computer and have the phone be able to read it in my app? In my development environment this would be uploading it through the file browser in Android Studio to the emulator. Using a physical device, one would upload it via USB. – Robert Jun 27 '22 at 04:11
  • I do not use the emulator much beyond running automated tests. On hardware, you have access to the cited directory using USB. Or, if you only need read access, the user could create a directory off of the root of what the Android SDK refers to as "external storage" (`/storage/emulated/0/` in your example above) and put the file in there. – CommonsWare Jun 27 '22 at 11:30

0 Answers0