I am an Android Studio app dev. I am a beginner and making a file manager app and having some problems in allowing user to delete a file. The code is in java.
I already have permissions in manifest and have taken runtime permissions.
I have already tried code like .delete(); and Boolean temp = File.delete();
In these methods it gave warning that it will be ignored
This is my current code:
final Button delete = findViewById(R.id.b1);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder deleteDialog = new AlertDialog.Builder(MainActivity.this);
deleteDialog.setTitle("Delete");
deleteDialog.setMessage("Do you really want to delete it?");
deleteDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < files.length; i++) {
if (selection[i]) {
deleteFileOrFolder(files[i]);
}
}
files = dir.listFiles();
filesFoundCount = files.length;
filesList.clear();
for (int i = 0; i < filesFoundCount; i++) {
filesList.add(files[i].getAbsolutePath());
}
textAdapter1.setData(filesList);
}
});
deleteDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
deleteDialog.show();
}
});
isFileManagerInitialised = true;
}
}
private void deleteFileOrFolder(File FileOrFolder) {
boolean isSuccess = false;
if (FileOrFolder.isDirectory()) {
if (FileOrFolder.list().length == 0) {
isSuccess = FileOrFolder.delete();
delete(getApplicationContext(), FileOrFolder);
} else {
String files[] = FileOrFolder.list();
for (String temp : files) {
File fileToDelete = new File(FileOrFolder, temp);
deleteFileOrFolder(fileToDelete);
}
if (FileOrFolder.list().length == 0) {
isSuccess = FileOrFolder.delete();
delete(getApplicationContext(), FileOrFolder);
}
}
} else {
isSuccess = FileOrFolder.delete();
delete(getApplicationContext(), FileOrFolder);
}
}
public static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[]{
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final String rootPath = String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
contentResolver.delete(Uri.parse(rootPath), where, selectionArgs);
if (file.exists()) {
contentResolver.delete(Uri.parse(rootPath), where, selectionArgs);
}
return !file.exists();
}
The problem with it currently is that it is crashing on deleting and it does not delete on opening again.
LogCat: --------- beginning of crash 2020-09-01 19:15:52.146 9248-9248/com.example.file_manager E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.file_manager, PID: 9248 java.lang.IllegalArgumentException: Unknown URL /storage/emulated/0/Download at android.content.ContentResolver.delete(ContentResolver.java:2278) at android.content.ContentResolver.delete(ContentResolver.java:2248) at com.example.filemanager.MainActivity.delete(MainActivity.java:287) at com.example.filemanager.MainActivity.deleteFileOrFolder(MainActivity.java:262) at com.example.filemanager.MainActivity.access$200(MainActivity.java:35) at com.example.filemanager.MainActivity$2$1.onClick(MainActivity.java:213)
Can someone tell me how to delete the file?
Can someone please help me for I am doing this for a hackathon and need it by today 1 september 2020, so if you come across please answer