MDobroch
MDobroch

Reputation: 71

Can't create folder and file

I'm trying to create folder in internal storage of the device, and have some problems with that. I can't create directory using :

   String rootPath=Environment.getExternalStorageDirectory().getPath()+"/test";
    File file=new File(rootPath);
     if(!file.exists()){
         file.mkdir();
    }

file.mkdir return false

I have such permissions:

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

Thank you very much for answers !

Upvotes: 1

Views: 2001

Answers (2)

MDobroch
MDobroch

Reputation: 71

So really, as you all guys said in Android Marshmallow (API 23) or higher we need to ask runtime permissions.

To solve this issue use call :

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

in your onCreate() method

Thanks all for your help !

Upvotes: 3

Enamul Haque
Enamul Haque

Reputation: 5063

You can use

  File sampleDir = new File(Environment.getExternalStorageDirectory(), "/test");
        if (!sampleDir.exists()) {
            sampleDir.mkdirs();
        }

Upvotes: 0

Related Questions