Android filename with white space cannot be opened

前端 未结 3 612
囚心锁ツ
囚心锁ツ 2021-01-01 06:04

My code works correctly while opening a file in sdcard. However, if I open a filename with white space then error occurs (example: Path - \"/sdcard/download/hello hi.jpg\").

相关标签:
3条回答
  • 2021-01-01 06:40

    Try:

    Uri uri = Uri.parse(paths);
    File file = new File(uri.getPath());
    

    Uri.parse fixes all the whitespace/backslash/illegal character issues in paths and produces a "good" uri.

    0 讨论(0)
  • 2021-01-01 06:53

    Replace %20 with space like below

    filePath =  filePath.replaceAll("%20"," ");
    

    This worked for me

    0 讨论(0)
  • 2021-01-01 06:57

    You need to escape the spaces. Try replacing " " with "\ "

    0 讨论(0)
提交回复
热议问题