5

I'm trying to update my app source code to Android 6.0 with the new runtime permissions.

But also if the user grants to app the storage permission, the app is not able to create directories in the 'onRequestPermissionsResult' method using the 'mkdirs'.

In the AndroidManifest.xml I put the 'uses-permission':

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xample.provasd"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="23" />

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

    <!-- application, activity, etc -->

Then in the MainActivity I deal with the permission request:

public void executeButton(View view){
        final String[] PERMISSIONS_STORAGE = { Manifest.permission.WRITE_EXTERNAL_STORAGE };
        //Asking request Permissions 
        ActivityCompat.requestPermissions(this,PERMISSIONS_STORAGE, 9);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        boolean writeAccepted = false;
        switch(requestCode){
        case 9:
            writeAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
            break;
        }
        if(writeAccepted){
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                File dir = new File(Environment.getExternalStorageDirectory()+"/"+"TestFolder");
                boolean b = dir.mkdirs();
                if(b){
                    Log.i("TAG", "WOW! "+dir+" created!");
                }else{
                    Log.e("TAG", "OPS! "+dir+" NOT created! To be sure: new dir exist? "+dir.exists());
                }
            }
        }       
    }

Launching the app on emulator, the request dialog is showed: allowing the permission the body of 'onRequestPermissionsResult' is executed (writeAccepted=true) BUT the folder is not created!

This is what appear in the LogCat:

I/TAG(9020): Permission ok
I/TAG(9020): Try to create dir: /storage/3143-1CEA/TestFolder
E/TAG(9020): OPS! /storage/3143-1CEA/TestFolder NOT created! To be sure: new dir exist? false

I don't understand the reason.

Please, someone can help me?

Thank you,

Mirko

Mirko
  • 135
  • 1
  • 8
  • I copied your code and successfully ran it on emulators for Nexus 5, 6, 9 using SDK Build Tools version 23.0.2. If you are not using the latest versions of the tools, you might consider upgrading to see if it makes a difference. When I first ran with an emulator created using an older version, `Environment.MEDIA_MOUNTED` returned "removed" even with the emulator configured for SD card (hw.sdCard=yes in emulator's config.ini). – Bob Snyder Feb 13 '16 at 07:00
  • Thank you so much for your reply. So, do you mean that it can be only an environment problem? I'm updating the SDK Build Tools and I will try to create another machine instance. PS. I found an awful workaround: after have obtained the permission, the folder creation fails but if I do a "Force stop" of app and I open it again... it works (without ask permission again). – Mirko Feb 13 '16 at 09:50
  • EDIT: updating SDK Build Tools to version 23.0.2 doesn't work! But I tested the code on a real 6.0 device and it works. – Mirko Feb 13 '16 at 15:49
  • I experimented more and can't explain why your code doesn't work on an emulator. There have been some reports of needing to restart the app after a grant of `WRITE_EXTERNAL_STORAGE` (see this [AOSP issue](https://code.google.com/p/android-developer-preview/issues/detail?id=2982)), but I think that problem was only seen in the preview version of Marshmallow. – Bob Snyder Feb 13 '16 at 18:03
  • Me too. I'm not able to unsterstand why this very simple code doesn't work on all my 6.0 emulated devices (every SDK component is updated). Thanks to your first comment, I tried on a physical device and it works (after 2 days of debugging). Thank you again for your time. – Mirko Feb 13 '16 at 18:59

1 Answers1

1

The problem may not lie in the code. If your phone is running on Android 6.0, look to see if your App has sufficient permissions.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
chaosxing
  • 11
  • 1
  • Please only answer in English on the English Stack Overflow. [→](https://translate.google.com/m/translate#auto/zh-CN/Please%20only%20answer%20in%20English%20on%20the%20English%20Stack%20Overflow.) "请只用英文回答有关英国堆栈溢出。" – Wai Ha Lee Mar 27 '16 at 13:57
  • The problem happens only on (my) 6.0 emulated devices: I tried on a physical device and it works like a charm (app has sufficient permissions). – Mirko Jul 01 '16 at 13:19