Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from arbitrary sd card folder

I am trying to read a dictionary on sd card (content of the folder is managed by third party app - ex. dropbox).

Although i granted my app permissions:

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

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

The app will be used only on one type of device Samsung Galaxy Tab 10.1 (Android 5.0.2) so I know the exact path to sdCard and the file I am trying to read: /storage/extSdCard/sip.png

When I call:

FileOutputStream out = new FileOutputStream("/storage/extSdCard/sip.png");

It throws:

exception java.io.FileNotFoundException: /storage/extSdCard/sip.png: open failed: EACCES (Permission denied)

I also tried:

grantUriPermission(getPackageName(), new Uri.parse("/storage/extSdCard"),
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

What do I need to do to get access to the file?

like image 445
M. Sestak Avatar asked Dec 07 '25 21:12

M. Sestak


1 Answers

What do I need to do to get access to the file?

You have no direct filesystem access to arbitrary locations on removable storage on Android 4.4+.

If possible, move the file elsewhere (e.g., external storage).

Or, use ACTION_OPEN_DOCUMENT and have the user choose the file, then use a ContentResolver and openInputStream() to work on the content identified by the Uri that you get in onActivityResult() from your ACTION_OPEN_DOCUMENT request.

like image 159
CommonsWare Avatar answered Dec 09 '25 19:12

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!