Search code examples
androiddirectoryandroid-10.0android-11scoped-storage

How to delete a folder and its content from SDcard on Android 10 and Android 11


I have an application running on android and I'm saving some files that the app needs to download in the sdcard, only if the user wants to use the external storage instead to use the internal storage, so according to the update for OS 10 and OS 11 we need to migrate all that content to the scoped storage to be used only for my app, the process to copy the folder and its content from ex /storage/EREKD34/MyFolder to /storage/EREKD34/Android/data//MyFolder works, but to avoid to have duplicated files I need to delete the folder "MyFolder" but the function to delete that File doesn't work, so do you have any another idea how to do this


Solution

  • I have to use

    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    intent.addCategory("android.intent.category.DEFAULT");
                    intent.setData(Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));
                    startActivityForResult(intent, REQUEST_CODE_STORAGE_PERMISSION);
    
    

    To give the permission to delete the folder with

    DocumentFile document = getDocumentFile(file, false, true); document.delete();