How delete Folder on Android API 30 and API 29

284 Views Asked by At

I created an app that creates folders based on the user's name, and there is an option to delete those folders, and even delete pre-made folders without my app.

My app works well on Android 8, 8.1 and 9, but it is different in newer versions of Android and I can no longer delete folders.

I requested access to memory and read and write using this

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

In Java code:

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

And this my code for delete folders:

        if (filePath.exists()) {
            if (filePath.delete()) {
                Toast.makeText(this, "Delete Folder Successfully.", Toast.LENGTH_LONG).show();
            }
        }

I create Folder like this:

        create_folder = (EditText) findViewById(R.id.creat_folder);
        submitButton = (Button) findViewById(R.id.submitButton);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                create_folder = new_created_folder.getText().toString();
            }
        });
    }

    String root = Environment.getExternalStorageDirectory().toString();
    File filePath = new File(root + create_folder);
    
    public void creatFile(View view){
        try {
            filePath.createNewFile();

            Toast.makeText(this,"Ready For Generate..",Toast.LENGTH_LONG).show();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

I'm sure that creating folders works properly, but I also want my software to be able to delete folders on the device itself (folders that were previously created without using my app)

I do not know what to do in Android 10 and 11 and is there any way to delete folders in these?

0

There are 0 best solutions below