Skip to content

Commit

Permalink
Fix PCAP file deletion on some Android versions
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele-f committed Jan 15, 2023
1 parent 4c0ce35 commit 20ef821
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,27 @@ public void showPcapActionDialog() {
}

private void deletePcapFile(Uri pcapUri) {
Log.d(TAG, "Deleting PCAP file" + pcapUri.getPath());
boolean deleted = false;

try {
deleted = (getContentResolver().delete(pcapUri, null, null) == 1);
} catch (UnsupportedOperationException | SecurityException e) {
e.printStackTrace();
// The getContentResolver().delete in some Android versions does not work, try to delete
// using file path first
String fpath = Utils.uriToFilePath(this, pcapUri);
if(fpath != null) {
Log.d(TAG, "deletePcapFile: path=" + fpath);

try {
deleted = new File(fpath).delete();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.d(TAG, "deletePcapFile: uri=" + pcapUri);

try {
deleted = (getContentResolver().delete(pcapUri, null, null) == 1);
} catch (UnsupportedOperationException | SecurityException e) {
e.printStackTrace();
}
}

if(!deleted)
Expand Down

0 comments on commit 20ef821

Please sign in to comment.