-2

If I create a folder in Android 7/9, the location is:

Environment.getExternalStorageDirectory() + "FOLDER_NAME"

But in Android 10 I want to delete the old folder, how I can delete the folder?

I have tried 3 methods so far;

1.

File newFile = new File(Environment.getExternalStorageDirectory() + "FOLDER_NAME");
newFile.delete();

2.

Get the folder list, then delete all files as shown in this thread:

How to delete a whole folder and content?

3.

Using MediaStore way I can not find the folder.

String[] projection = null;

String selection;

String[] selectionArgs;

Cursor cursor = null;

projection = new String[]{ MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.TITLE};

selection = MediaStore.Files.FileColumns.DATA + " like ?";

selectionArgs = new String[]{"%" + "FOLDER_NAME" + "/%"};

Uri external = null;

for (String volumeName : MediaStore.getExternalVolumeNames(context)) {

    external = MediaStore.Files.getContentUri(volumeName);
    break;

}

cursor = context.getContentResolver().query(
MediaStore.Files.getContentUri("external"),
projection,
selection,
selectionArgs,
null);

if(cursor.moveToFirst()) {

Uri fileUri = Uri.withAppendedPath(external,      cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)));

    ContentResolver resolver = context.getContentResolver();

    resolver.delete(fileUri, null, null);

}

Is there any way I can delete the old folder in Android 10?

Scott Evans
  • 374
  • 1
  • 10

2 Answers2

1

Maybe a file manager app for Android Q can do the job.

Otherwise use Storage Access Framework.

Take ACTION_OPEN_DOCUMENT_TREE to let the user choose the parent of your directory.

With obtained write permissions for the parent you can now delete any child.

blackapps
  • 8,011
  • 2
  • 11
  • 25
0

If you have URI access to that folder, then you can delete it recursively (along with its children) with Simple Storage:

// We want to delete folder /storage/emulated/0/Video/Horror
val folder = DocumentFileCompat.fromSimplePath(context, basePath = "Video/Horror")
// Set childrenOnly to true if you want to delete its children only
val success = folder?.deleteRecursively(context, childrenOnly = false)
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87