Abhi Panchal
Abhi Panchal

Reputation: 11

How can i create Directory in /storage/emulated/0

What i write this code when is first activity open and i also get user permission at runtime.

Permissions :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"
        tools:ignore="ProtectedPermissions" />

Code :

        private void createFolder()
        {
               File myFolder=new File(Environment.getExternalStorageDirectory(),"MyFolder");
               if (!myFolder.exists()){
                   myFolder.mkdirs();
                   if (!myFolder.exists()&&myFolder.isDirectory()){
                       File myChild=new File(musicVideo.getAbsolutePath(),"MyChild");
                            if (!myFolder.exists()){
                                myChild.mkdirs();
                            }
                        }
               }else {
                   if (!myFolder.exists()&&!myFolder.isDirectory()){
                     File myChild=new File(myFolder.getAbsolutePath()+File.separator+"MyChild");
                        if (!myFolder.exists()){
                            myChild.mkdirs();
                        }
                   }else {
                       Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
                   }
                     Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
              }
        }

Upvotes: 1

Views: 2273

Answers (1)

Mab
Mab

Reputation: 412

All that is left is to request user Permission at Runtime

You can use a Utility method like below: to check that Permission is Granted before performing action

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

make sure the activity implements onRequestPermissionsResult

EDIT Due to your request in the comment section, there are alot of errors in your conditional tests. To create directory and sub-directory MyFolder and MyChild with createFolder()

Use:

private void createFolder()
    {
        File myFolder=new File(Environment.getExternalStorageDirectory(),"MyFolder");
        if(!myFolder.exists() || myFolder.isFile()){
            if(myFolder.isFile()){
                Toast.makeText(getApplicationContext(), "'MyFolder' exists as file", Toast.LENGTH_LONG).show();
                return;
            }
            try{
                myFolder.mkdir();
                File myChild=new File(myFolder.getAbsolutePath()+File.separator+"MyChild");
                myChild.mkdir();
                
                Toast.makeText(getApplicationContext(), "Directories created successfully", Toast.LENGTH_SHORT).show();
            } catch(Exception e){
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            
            
        }
    }

And in your implementation :

isStoragePermissionGranted();
createFolder();

The first line verifies storage permission is granted and the second creates the folders

Upvotes: 1

Related Questions