Search code examples
javaandroidfirebaseandroid-studioandroid-asynctask

Directory is not being created even after seeking Storage permission


I am just trying to create a directory seeking required permission including permissions in manifest but still directory is not being created.

Here is my code below-

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(haveStoragePermission()) {

        File new_folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Images");
        if (!new_folder.exists()) {
            new_folder.mkdirs();
            boolean k=   new_folder.mkdirs();
         if (k)
         {
          Toast.makeText(this,"Directory created successfully",Toast.LENGTH_LONG).show();
         }
         else Toast.makeText(this,"Failed to create directory",Toast.LENGTH_LONG).show();

        }
        
    }


}

Solution

  • The problem was with Android 10 permissions, it's bit different here to seek permission in manifest where we need to add following line to manifest.xml- android:requestLegacyExternalStorage="true" as mentioned below-

    <application>
    android:requestLegacyExternalStorage="true" 
    </application>