0

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;
        }

    }
MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
  • Look here your solution http://stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 – Sohail Zahid Oct 04 '16 at 10:49
  • `mediaStorageDir.mkdirs(); Log.d("FolderCreation", "created successfully");`. Well that is of course not true as mkdirs() can fail and return false. Check what it returns. – greenapps Oct 04 '16 at 10:58
  • 1
    Did you ask android runtime storage permission from user before creating this folder? – Developer Oct 04 '16 at 11:13
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Oct 04 '16 at 11:13

1 Answers1

1

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.

Hendrik J.
  • 11
  • 1