-4

I have developing my android app and testing it in the lowest supported SDK, which is API 18 Jellybean through an emulator (Genymotion). There is a use case where I have to download a pdf file and save it in the external storage. This works as expected in the API 18 emulator without any trouble. Then i decided to test it in my own device, which is a Huawei Honor6x with API 24, but i got a FileNotFoundException. After debugging i figured out that its not creating the directory in the external storage. To cross check if this is a problem specific to my own device, I installed 2 other emulators with API 24 (Pixel XL and Galaxy S7). I got the same error. Is there any security features blocking me from creating a folder in never versions?

here is my code

I have the following permissions

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

And here is the code where i create the directory

File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/notespal");
    if (!directory.exists()){
        boolean created = directory.mkdirs();
    }

Appreciate your assistance since i have been stuck in this for few days now.

PraZ
  • 57
  • 1
  • 8

1 Answers1

3

make sure the android.permission.WRITE_EXTERNAL_STORAGE permission is granted

int code = context.getPackageManager().checkPermission(
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 
    context.getPackageName());
if (code == PackageManager.PERMISSION_GRANTED) {
   // todo create directory
}
Arpan Sarkar
  • 2,301
  • 2
  • 13
  • 24
  • Thanks for your quick reply. Tested the permission. You were right. It seems the permission is not set. Do i have to do anything else other than setting the permissions in the manifest file to get this working? – PraZ Feb 09 '18 at 05:45
  • Refer this link https://developer.android.com/training/permissions/requesting.html – Arpan Sarkar Feb 09 '18 at 05:48