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?