2

Following code is working with other APIs but 26

public static boolean cretaeDir()
{
    String dir = Environment.getExternalStoragePublicDirectory(Environment.MEDIA_SHARED).getAbsolutePath();
    java.io.File folder = new java.io.File(dir + "/SomeFolderName");
    if (!folder.exists()) {
        try{
            if(folder.mkdir()) {
                Log.e("Creating Folder", "Success");
                return true;
            } else {
                Log.e("Creating Folder", "Failed");
                return false;
            }
        } catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }else{
        return true;
    }
}

Before some one ask, Yeah the permissions have been granted and also uses-permission present in the Manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.vending.BILLING" />


<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />


<application/>

Test Device is Samsung S8, Running 8.0.0

Yupi
  • 4,402
  • 3
  • 18
  • 37
Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
  • I know you have written permissions have been granted but does this include a `WRITE_EXTERNAL_STORAGE runtime permission`? – Rene Ferrari May 23 '18 at 23:15
  • Yes this function is only called when user have granted the permission. – Ussaid Iqbal May 23 '18 at 23:17
  • Just to be certain you asked permission as described in the answer to this question :: https://stackoverflow.com/questions/47217725/failed-to-create-directory-in-android-oreo-api-26 – Barns May 24 '18 at 02:55

4 Answers4

1

try this

File fsd = Environment.getExternalStorageDirectory();
String filePath = fsd.getAbsolutePath() + "/SomeFolderName";

File dir = new File(filePath);
if(dir.isDirectory==false || !dir.exists()){
    dir.mkdirs();
}
Deepak kaku
  • 1,218
  • 9
  • 15
1

ok, so this worked for me.

RequestPermission Methods, when asking for permission, ask WRITE_EXTERNAL_STORAGE before anything else. Try it...

MainActivity.kt

// First permission to be asked
requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
// Then all other permissions

This is working for all API inclusive & levels above 26. TBH, I don't know how, but it working.

0

You are not using a valid folder type. You have to choose from the following options

The type of storage directory to return. Should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, DIRECTORY_DCIM, or DIRECTORY_DOCUMENTS. May not be null.

Your code should be something like this

String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22
0

I have fixed it by the following way as I have no other option left and I have tried all the suggested solutions. Maybe the error is device dependent or whatever, but it has nothing to do with API 26 as I have upgraded one of my test devices to API 26 and the old function did worked.

The following code is working on all devices.

 File file = MyApplication.getInstance().getDir("BUZZ",MODE_PRIVATE);
 cacheDir = new File(file.getAbsoluteFile()+File.separator+IMAGE_DIRECTORY_NAME);

Happy Coding :-)

Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
  • Yes this works as you now use a private folder for your app. For such a folder you do not need any permission in manifest or at runtime. So you did it wrong with the permissions. – greenapps May 24 '18 at 07:36
  • `cacheDir = new File(file.getAbsoluteFile()+File.separator+IMAGE_DIRECTORY_NAME);` That is ugly code. Change to `cacheDir = new File(file, IMAGE_DIRECTORY_NAME);` – greenapps May 24 '18 at 07:37
  • Yeah as I have tried everything but didn't worked for me so the only option I see is use Applications Private Folder. And the permissions are removed from the manifest file as I have changed to private folder. – Ussaid Iqbal May 24 '18 at 07:48
  • Did you also remove the code used for asking the user to confirm the requested prmissions at runtime? – greenapps May 24 '18 at 08:03