I am trying to delete some files on the SD card , but i ran out of ideas..
I tried File.delete()
method, then I have tried file.getCanonicalFile().delete()
and many more .. My application can just delete files on the device storage . I have got the permissions defined in the Manifest file that is required like below.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and I am requesting for permission in the code also:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(this)
.setTitle(R.string.read_storage_message)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MainActivity.READ_STORAGE_PERMISSION_ID);
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AppKiller.kill();
}
}
).show();
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(this)
.setTitle(R.string.write_storage_message)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MainActivity.STORAGE_PERMISSION_ID);
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AppKiller.kill();
}
}
).show();
}
}
I am using the following method to delete the files, but as i mentioned before, I've used plenty of solutions found in Stack-overflow and others.
protected boolean delete(Context context) throws Exception {
boolean delete = false;
File file = new File(Environment.getExternalStorageDirectory().getPath() + getName());
if (file.exists()) {
delete = file.delete();
if (!delete) {
delete = file.getCanonicalFile().delete();
}
if (!delete) {
String deleteCmd = "rm -r " + file.getAbsolutePath();
Runtime runtime = Runtime.getRuntime();
runtime.exec(deleteCmd);
}
}
return delete;
}
could it be because i am asking for READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
permissions, but in code i am just getting READ_EXTERNAL_STORAGE
granted and the other is getting ignored from Android(it won't show permission WRITE_EXTERNAL_STORAGE
popup with allow-deny options after I accept the first one) . Isn't it because they have the same permission's level in android ?
what could be wrong ?
Yes you cannot delete files from SD card on modern Android versions anymore using the File class.
Use the Storage Access Framework instead.
Have a look at Intent.OPEN_DOCUMENT_FILE
and OPEN_DOCUMENT_TREE
.