MikeKeepsOnShine
MikeKeepsOnShine

Reputation: 1750

Android Mashmallow can't create folders on StoragePublicDirectory

I'm working on an application built for Android SKD version 22.

In this application folders are created using this way:

String FileDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES) + "/" + app_name + "/" + prefix;

File mediaStorageDir = new File(FileDir);

if (!mediaStorageDir.exists()){
          mediaStorageDir.mkdirs();
          Log.d("FolderCreation", "created successfully");
}else{
          Log.d("FolderCreation" , "Already exists"); 

}

It was working well until I had to compile and run it on Android 6 (SDK v.23).

Now the folders is not created. Is there something that has changed with Android 6 regarding the folder "storage/emulated/0"?

Do I have to change the directory when I work on Android 6?

EDIT -> SOLVED

With Android 6 (SDK v23) I must ask the permissions runtime, showing a confirm dialog.

Here the code I use that works.

//create a method that check the permissions
public static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

In the onCreate():

//list of permissions that you need to ask
String[] permissions = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION};

//use the method to check if the user needs to confirm the permissions
if(!hasPermissions(this,permissions)){
                ActivityCompat.requestPermissions(this, permissions, PERMISSION_IDENTIFIER);
            }else{
                //permission already granted, hooray!
            }

Add onRequestPermissionsResult to manage the choose of the user when the dialog is shown:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch(requestCode){
            case PERMISSION_IDENTIFIER:
                    //your actions
                break;
        }

    }

Upvotes: 0

Views: 70

Answers (1)

Hendrik J.
Hendrik J.

Reputation: 11

Android 6 introduced a new version of permission management. Users can now give and take permissions for apps.

See Android - Marshmallow.

Check youre app permissions in the emulator and look out as

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

might not be given by the user.

Upvotes: 1

Related Questions