android - file.exists() returns false for existing file (for anything different than pdf)

前端 未结 4 797
感动是毒
感动是毒 2020-12-31 05:19

Both files are present on the sdcard, but for whatever reason exists() returns false the the png file.

//String path = \"/mnt/sdcard/Android/data/com.gemoro.         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 05:44

    1 You need get the permission of device

    Add this to AndroidManifest.xml

    
    

    2 Get the external storage directory

    File sdDir = Environment.getExternalStorageDirectory();
    

    3 At last, check the file

    File file = new File(sdDir + filename /* what you want to load in SD card */);
    if (!file.canRead()) {
        return false;
    }
    return true;
    

    Note: filename is the path in the sdcard, not in root.

    For example: you want find

    /mnt/sdcard/Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png
    

    then filename is

    ./Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png
    

    .

提交回复
热议问题