0

I have an application that in the login I download images from my server. in the first login, everything is work fine but when I uninstall the application and install it again I get this exception:

java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.gilapp.android.app/files/images/book_118.png: open failed: EACCES (Permission denied)

and can't save the image.

this happen while I'm trying to create output stream:

OutputStream output = new FileOutputStream(filePath);

The images download fine just when I do the second installation after I go to App info -> Storage -> clear data

Another important point is that it happens only in Samsung's tablet: SM-T800 (API level 23)

In other devices it's not happening, the devices that I test: Samsung SM-G900H (API level 23), Asus P028 (API level 24), Samsung SM-G950F (API level 28)

javadroid
  • 1,421
  • 2
  • 19
  • 43
gil cohen
  • 333
  • 2
  • 8
  • 21

4 Answers4

12

If your app is pointing to the android version 10.

In the manifest include under the application add


android:requestLegacyExternalStorage="true"


Note that this workaround no longer works in Android 11.

Atai Ambus
  • 89
  • 1
  • 2
  • 10
viveksuggu
  • 737
  • 2
  • 7
  • 12
1

You need to get Run-time permission for write in storage. before saving your files check the permission.also there are some library for this work: Android-Arsenal

is better to use a FileProvider for accessing files in storage.

javadroid
  • 1,421
  • 2
  • 19
  • 43
  • thanks, you're right, when I use run-time permission I don't get this exception. but you know why it happens only in Samsung's tablet: SM-T800 (API level 23) – gil cohen Jan 21 '20 at 14:47
  • your welcome, the runetime permission is added from api level 23 :) – javadroid Jan 22 '20 at 04:26
1

When you uninstall your application all files in getExternalFilesDir()are removed hence the /images directory has gone

After reinstall you can again write files to that directory for which you do not need any permission.

Not at runtime. Not in manifest.

But you want them in the ..../images directory.

You have to care for that directory yourself.

Check if the directory exists and then create it before you try to put a file in it.

blackapps
  • 8,011
  • 2
  • 11
  • 25
1

First, check whether you have implemented scoped storage logic in the app. You can also use android:requestLegacyExternalStorage="true" But this legacyStoragePermission is limited to version 10. You need to implement scoped logic.

Also, check whether your targetSDKVersion value is 30 or greater or not, this is needed if you are using the app in Device android version 30 or more.

Bharat Lalwani
  • 1,277
  • 15
  • 17