- if (contentDisposition.isEmpty()) {
- fileNameString = "downloadfile.bin";
- } else {
- // Extract `fileNameString` from `contentDisposition` using the substring beginning after `filename="` and ending one character before the end of `contentDisposition`.
+ if (contentDisposition.isEmpty()) { // If `contentDisposition` is empty, use the last path segment of the URL as the file name.
+ Uri downloadUri = Uri.parse(urlString);
+ fileNameString = downloadUri.getLastPathSegment();
+ } else { // Extract `fileNameString` from `contentDisposition` using the substring beginning after `filename="` and ending one character before the end of `contentDisposition`.