简体   繁体   中英

Can't open a file which has a question mark in its name

In my Android app, users can download files. The app modify the downloaded file right after the download completes. However, whenever the downloaded file's name includes a question mark somewhere in it, the downloaded file cannot be reached and thus cannot be modified. Related code is as follows:

String pathToFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + document.getTitle();

// Example pathToFile: /storage/emulated/0/Download/Recording ? 20171227.mp3
File downloadedFile = new File(pathToFile);

if (!downloadedFile.exists()) {
    // Always gets here if filename includes at least one '?'
}

I try adding "file://" prefix to pathToFile , converting pathToFile to Uri or URI, but nothing worked. It always says file doesn't exist. Any help is appreciated.

ADDITIONAL INFO: The document filename is retrieved from the internet and I save the document using the same filename. I can see the file with same filename in the Downloads folder, however, I cannot open it programmatically.

As recommended by commentators, I replace # , % and ? with empty string. The following characters don't cause a problem: !@$^&*()[]{}|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM